标签: nsattributedstring

将NSMutableAttributedString写入/读取到Plist

好的,我们在这里:

  • 我有一个 NSTextView
  • 我得到它的NSMutableAttributedString满足
  • 我无法将其读/写到plist

通过使用Rob的代码(将自定义属性保存在NSAttributedString中),我已经取得了一些进展(我正在设法将数据写入磁盘),但是我无法恢复它(= NSKeyedUnarchiverreturn nil)。


编码方式:

// where MAS --> NSMutableAttributedString

NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
Run Code Online (Sandbox Code Playgroud)

解码:

NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?任何可能的解决方法(即使不能使用NSCoder,我都怀疑它可以与RTF一起使用...)!

cocoa rtf objective-c nstextview nsattributedstring

3
推荐指数
1
解决办法
843
查看次数

在iOS 6中使用格式化键入UITextView

嗯,这似乎很愚蠢,它无法完成 - 您可以设置一个预定义的属性字符串,以显示在带有格式的文本视图上,但您无法设置要应用于实时键入的常规格式UITextView.

例如,如果我在文本视图中输入任何内容之前执行此操作:

NSString *string = @"Hello world";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 50.f;
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:string];
[aStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[string length])];
mainTextView.attributedText = aStr;
Run Code Online (Sandbox Code Playgroud)

然后格式化显示,当我用键盘编辑文本时,格式仍然存在.

但是,如果我想从空白开始UITextView并将格式应用于用户将在视图中键入的任何内容,该怎么办?

我试过设置string = @""或string = @" ",但这些都没有保留格式.是否真的没有办法将格式应用于用户输入?具有属性字符串属性的UITextView的重点是什么?

根据文档,将完全擦除的text属性设置UITextView为由属性字符串完成的任何格式化.因此无论解决方案是什么,它都不必直接设置textView.text属性,而只是调整attributedText属性.

iphone objective-c nsattributedstring ios ios6

3
推荐指数
1
解决办法
7439
查看次数

逐个字符地向NSMutableAttributedString添加属性

这是我的情况:

我NSMutableAttributedString在文本视图中没有属性.每当用户按下退格键时,我都不希望删除一个字符,我希望它能够被删除,就像生产力套件的"Track Changes"功能一样.我希望用户能够在此之后继续正常输入.这是我开始的方式:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (text.length == 0 && textView.text.length == 0) return YES;

if (text.length == 0 && !([[textView.text substringFromIndex:textView.text.length - 1] isEqualToString:@" "] || [[textView.text substringFromIndex:textView.text.length - 1] isEqualToString:@"\n"])) {

    textView.attributedText = [self strikeText:textView.attributedText];

    textView.selectedRange = NSMakeRange(textView.attributedText.length - 1, 0);

    return NO;
}
return YES;
}

- (NSAttributedString *)strikeText:(NSAttributedString *)text
{
NSRange range;
NSMutableAttributedString *returnValue = [[NSMutableAttributedString alloc] initWithAttributedString:text];

if (![text attribute:NSStrikethroughStyleAttributeName atIndex:text.length - 1 effectiveRange:&range]) {
    NSLog(@"%@", NSStringFromRange(range));
    NSLog(@"%@", …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios nsrange

3
推荐指数
2
解决办法
2万
查看次数

一个UITableViewCell文本标签中的两种文本颜色

我有一个UITableViewCell由两个单词组成的文本标签.

如何更改单词的颜色; 将第一个单词设为绿色,将第二个单词设为红色?

uitableview nsattributedstring ios

3
推荐指数
2
解决办法
4278
查看次数

下划线文本 - 具有不同字体大小的交叉范围中的线条粗细

我正在使用textStorage的属性UITextView.我有字符串和我班级的对象数组TextFormattingElement.此类的一个实例由NSRange(必须在文本中应用此元素)和一些格式化参数组成:

@interface TextFormattingElement : NSObject

@property (nonatomic) NSRange range;
@property (nonatomic, strong) NSString *fontName;   //e.g. @"TimesNewRomanPSMT"
@property (nonatomic) int fontSize;
@property (nonatomic, strong) UIColor *fontColor;
@property (nonatomic) BOOL isBold;
@property (nonatomic) BOOL isItalic;
@property (nonatomic) BOOL isUnderlined;
@property (nonatomic) BOOL isStriked;

@end
Run Code Online (Sandbox Code Playgroud)

现在我遍历这个数组,并连续将这些元素应用到textView的textStorage.我用这个方法:

-(void)setFontWithName:(NSString*)name AndSize:(float)fontSize AndTextColor:(UIColor*)textColor AndIsBold:(BOOL)isBold AndIsItalic:(BOOL)isItalic AndIsUnderlined:(BOOL)isUnderLined andIsStriked:(BOOL)isStriked ToRange:(NSRange)rangeToSet{
   __block UIFont *font = [UIFont fontWithName:name size:fontSize];
   __block UIFontDescriptor *fontDescriptor = [font fontDescriptor];

   [textView.textStorage enumerateAttributesInRange:rangeToSet options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios textkit

3
推荐指数
1
解决办法
1745
查看次数

具有不同NSMutableParagraphStyle的Nsattributedstring

我有一个带有多个段落的属性字符串.
我已经给了FirstLineHeadIndent = 2.12.
现在我想要给第FirstLineHeadIndent=01段和FirstLineHeadIndent=2第2段中的属性字符串.

如果我设置属性像

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing =Line_space;
        paragraphStyle.firstLineHeadIndent =2;
        paragraphStyle.headIndent =margin_space;
        paragraphStyle.tailIndent =-margin_space;
        paragraphStyle.paragraphSpacing=paragraph_space;

        NSDictionary *ats = @{
                              NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize],
                              NSParagraphStyleAttributeName : paragraphStyle,
                              };
Run Code Online (Sandbox Code Playgroud)

它将为paragaraph提供顶部空间.请帮我.我正在上传图片以获得更多帮助: - 在此输入图像描述

attributes nsattributedstring ios

3
推荐指数
1
解决办法
2303
查看次数

以编程方式更改UIButton的标题颜色,其标题设置为iOS 7中的属性

我已经UIButton以UITableView编程方式添加了一个.我的问题是我需要给出Letter Spacing以及需要更改按钮标题颜色.我Letter Spacing使用下面的代码给出了按钮标题文本,但标题文本颜色没有变化.

这是我的代码:

btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLogin.frame = CGRectMake(0, 0, 320, 42);
btnLogin.titleLabel.font = customFont;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"LOG IN"];

[attributedString addAttribute:NSKernAttributeName
                         value:@(spacing)
                         range:NSMakeRange(0, [@"LOG IN" length])];

[btnLogin setAttributedTitle:attributedString forState:UIControlStateNormal];

btnLogin.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_highlighted.png"]];

[btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //it's not working.

[btnLogin addTarget:self action:@selector(onClickLogin) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:btnLogin];
[cell.contentView bringSubviewToFront:btnLogin];
Run Code Online (Sandbox Code Playgroud)

你能帮我解决一下如何更改按钮标题颜色吗?谢谢.

iphone uibutton nsattributedstring ios7

3
推荐指数
1
解决办法
6486
查看次数

索引字符串中的上标美分

我试图让我的标签看起来像这样:

在此输入图像描述

但是使用属性字符串,我设法得到了这个结果:

在此输入图像描述

我的代码:

NSString *string = [NSString stringWithFormat:@"%0.2f",ask];

NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];

[buyString addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:15.0]
                  range:NSMakeRange(2, buyString.length - 2)];

self.labelBuy.attributedText = buyString;
Run Code Online (Sandbox Code Playgroud)

如你所见,点后的数字,保持在下面,我想将它们作为第一个例子弹出到顶部.有没有办法设置属性字符串框架?

objective-c nsattributedstring

3
推荐指数
1
解决办法
2304
查看次数

防止NSAttributedString中的换行符

我认为当你有一组不想要断线的单词时,这是一个常见的问题.

有时这些单词之间的字符是空格或连字符等.在我的情况下,它是一个点:)

这是我的文字50.0/80.0

最后,我使用尺寸标签并测量了特别需要的字符串空间:

UIFont *fontAwardNumber = [UIFont fontWithName:@"DIN-Bold" size:20];

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize labelSize = (CGSize){customCell.awardValueLabel.bounds.size.width, FLT_MAX};
CGRect rectNeededForAwardNumber = [awardNumber boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: fontAwardNumber} context:context];
if (rectNeededForAwardNumber.size.height > customCell.awardValueLabel.bounds.size.height) {
    //We need to add a breakline
    NSRange range = [awardNumber rangeOfString:@"/"];
    if (range.location != NSNotFound) {
        awardNumber = [awardNumber stringByReplacingCharactersInRange:range withString:@"/\n"];
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到了其他解决方案,例如用空格或连字符代替不可破坏的字符:

防止部分NSAttributedString中的换行符

但我的问题更为笼统,NSAttributedString是否提供了将一组单词定义为不可破解的内容?或者,对于一般词汇,有没有更简单的方法呢?

nsattributedstring ios

3
推荐指数
1
解决办法
1483
查看次数

Swift中的项目符号列表-iOS

我需要在iOS应用程序中将项目符号文本添加到textView中。我在看这个链接和这一个,并跟随他们的想法。这是我的代码:

let paragraph = NSMutableParagraphStyle()
paragraph.firstLineHeadIndent = 15
paragraph.headIndent = 15

attributes = [
    NSAttributedStringKey.paragraphStyle: paragraph
]

attributedString = NSAttributedString(string: "\u{2022} Some text some text some text some text some text some text", attributes: attributes)
finalText.append(attributedString)
Run Code Online (Sandbox Code Playgroud)

我需要的是使文本缩进上面的文本的开头。就像图片中的一样:

在此处输入图片说明

我得到的是带有项目符号起点的缩进文本。

在此处输入图片说明

nsattributedstring ios swift

3
推荐指数
4
解决办法
1545
查看次数