修改 UILabel 文字中某段文字的颜色和大小 - iOS

一段文字中的某一段内容需要将其变得更加突显出来,为了复杂多余的操作流程,便通过如下的方法将内容进行调整,更加简便方便。

GitHub - 声明

GitHub - 实现

/**
 改变 label 文字中某段文字的颜色和大小
 label      传入的文本内容(注:传入前要有文字)
 oneIndex   从首位文字开始
 endIndex   至末位文字结束
 color      字体颜色
 size       字体字号
 */
+ (void)YHLabelAttributedString:(UILabel *)label firstText:(NSString *)oneIndex toEndText:(NSString *)endIndex textColor:(UIColor *)color textSize:(CGFloat)size;
+ (void)YHLabelAttributedString:(UILabel *)label firstText:(NSString *)oneIndex toEndText:(NSString *)endIndex textColor:(UIColor *)color textSize:(CGFloat)size {
    // 创建 Attributed
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
    // 需要改变的首位文字位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:oneIndex].location;
    // 需要改变的末位文字位置
//    NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + 1;
    NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + [[noteStr string] rangeOfString:endIndex].length;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, endLoc - firstLoc);
    // 改变颜色
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    // 改变字体大小及类型
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:size] range:range];
    // 为 label 添加 Attributed
    [label setAttributedText:noteStr];
}

 


以上便是此次分享的全部内容,希望能对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Swift,可以使用NSAttributedString类来更改指定文字颜色。可以使用属性典,设置文字颜色属性,并将其设置为NSAttributedString对象的属性,即可更改指定文字颜色。 ### 回答2: 要想在Swift更改一段文字指定两个文字颜色,我们可以使用NSMutableAttributedString来实现。NSMutableAttributedString是NSAttributedString的子类,它允许我们对NSAttributedString进行修改和编辑。 首先,我们需要将一段文字转换为NSMutableAttributedString类型的对象。可以使用NSAttributedString的构造函数来实现这一点,将要修改文字作为参数传递进去。接下来,我们可以使用addAttributes(_:range:)方法来为指定的文字添加属性。 在我们的情况下,我们要更改两个文字颜色。我们可以使用NSAttributedString.Key.foregroundColor这个键来指定文字颜色属性。我们需要为NSMutableAttributedString的指定范围添加这个属性。范围可以使用range(of:)方法来确定。 下面是一个示例代码,展示了如何使用NSMutableAttributedString来更改一段文字指定两个文字颜色: ```swift let text = "Hello World" let attributedText = NSMutableAttributedString(string: text) let firstWord = "Hello" let secondWord = "World" let rangeOfFirstWord = (text as NSString).range(of: firstWord) let rangeOfSecondWord = (text as NSString).range(of: secondWord) attributedText.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], range: rangeOfFirstWord) attributedText.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], range: rangeOfSecondWord) // 将attributedText赋值给显示文本的UILabel或UITextView ``` 以上代码,我们创建了一个NSMutableAttributedString对象attributedText,并将原始文字"Hello World"传递给它。然后,我们找到"Hello"和"World"在attributedText的范围,并为它们分别设置了红色和蓝色的颜色属性。最后,我们可以将attributedText赋值给显示文本的UILabel或UITextView,以显示已更改颜色的文本。 这样,我们就成功地使用Swift更改了一段文字指定两个文字颜色。 ### 回答3: 要将一段文字的指定两个文字颜色进行更改,我们可以使用NSAttributedString来实现。 首先,我们需要创建一个NSMutableAttributedString对象,它是NSAttributedString的可变版本。然后,我们可以使用addAttributes(_:range:)方法来为指定的文字添加属性。 下面是一个示例代码,展示了如何将一段文字的指定两个文字改变颜色: ```swift // 原始文本 let text = "Hello, World!" // 创建NSMutableAttributedString对象 let attributedText = NSMutableAttributedString(string: text) // 设置指定文字的属性 let firstWordRange = (text as NSString).range(of: "Hello") let secondWordRange = (text as NSString).range(of: "World") let textColor = UIColor.blue attributedText.addAttributes([NSAttributedString.Key.foregroundColor: textColor], range: firstWordRange) attributedText.addAttributes([NSAttributedString.Key.foregroundColor: textColor], range: secondWordRange) ``` 在上面的代码,我们首先创建了一个符串"Hello, World!"。然后,我们使用NSMutableAttributedString将其转换为可变的属性符串对象。 接下来,我们使用NSString的range(of:)方法找到指定文字的范围。然后,我们使用addAttributes(_:range:)方法为这些范围内的文字添加属性,其属性为指定的文本颜色(这里设置为蓝色)。 这样,我们就成功地将一段文字的指定两个文字颜色进行了更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值