如何在iphone中突出显示所选文本/字符串

时间:2013-05-14 06:35:26

标签: iphone ios objective-c text

我有一个字符串'专家销售经理'作为单元格标题,我想只在iphone的单元格标题中突出显示'销售'。
请建议我这样做的方法。

3 个答案:

答案 0 :(得分:0)

您可以将NSMutableAttributedString用于ios 6或更高版本。

   NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"OneTwoThreeFour"];
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:20] range:NSMakeRange(3,5)];

并将您的属性文本设置为元素..

label.attributedText = string;

答案 1 :(得分:0)

iOS 6 以来,UIKit支持绘制属性字符串

来自UILabel

@property(nonatomic, copy) NSAttributedString *attributedText;

您只需要建立NSAttributedString。基本上有两种方式:

  1. 附加具有相同属性的文本块 - 为每个部分创建一个NSAttributedString实例并将其附加到一个NSMutableAttributedString

  2. 从普通字符串创建属性文本,然后为给定范围添加属性 - 找到您的数字范围(或其他)并在其上应用不同的突出显示属性。

答案 2 :(得分:0)

您可以使用NSMutableAttributedString

NSString *infoString=@"This is an example of Attributed String";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];

UIColor *_black=[UIColor blackColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, _stringLength)];