HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 在单个UILabel中的粗体和非粗体文本?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在uiLabel中包含粗体和非粗体文本?

我宁愿不使用UIWebView ..我也读过这可能是可能使用NSAttributedString,但我不知道如何使用它。有任何想法吗?

苹果在几个应用程序中实现了这一点;
示例屏幕截图:

谢谢!
– Dom

解决方法

更新Swift3

在Swift中,我们不必处理iOS5旧的东西,除了语法更短,所以一切变得非常简单:

func attributedString(from String: String,nonBoldRange: NSRange?) -> NSAttributedString {
    let fontSize = UIFont.systemFontSize
    let attrs = [
        NSFontAttributename: UIFont.boldSystemFont(ofSize: fontSizE),NSForegroundColorAttributename: UIColor.black
    ]
    let nonBoldAttribute = [
        NSFontAttributename: UIFont.systemFont(ofSize: fontSizE),]
    let attrStr = NSMutableAttributedString(String: String,attributes: attrs)
    if let range = nonBoldRange {
        attrStr.setAttributes(nonBoldAttribute,range: rangE)
    }
    return attrStr
}

用法

let targetString = "updated 2012/10/14 21:59 PM"
let range = NsmakeRange(7,12)

let label = UILabel(frame: CGRect(x:0,y:0,width:350,height:44))
label.BACkgroundColor = UIColor.white
label.attributedText = attributedString(from: targetString,nonBoldRange: rangE)
label.sizeToFit()

奖金:国际化

有些人评论国际化。我个人认为这是超出这个问题的范围,但对于教学目的,这是我会怎么做

// Date we want to show
let date = Date()

// Create the String.
// I don't set the locale because the default locale of the formatter is `NSLocale.current` so it's good for internationalisation :p
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timestyle = .short
let targetString = String(format: NSLocalizedString("update %@",comment: "updated String format"),formatter.String(from: datE))

// Find the range of the non-bold part
formatter.timestyle = .none
let nonBoldRange = targetString.range(of: formatter.String(from: datE))

// Convert Range<Int> into NSRange
let nonBoldNSRange: NSRange? = nonBoldRange == nil ?
    nil :
    NsmakeRange(targetString.distance(from: targetString.starTindex,to: nonBoldRange!.lowerBound),targetString.distance(from: nonBoldRange!.lowerBound,to: nonBoldRange!.upperBound))

// Now just build the attributed String as before :)
label.attributedText = attributedString(from: targetString,nonBoldRange: nonBoldNSRangE)

结果(假设英语和日语Localizable.Strings可用)

iphone – 在单个UILabel中的粗体和非粗体文本?

iphone – 在单个UILabel中的粗体和非粗体文本?

iOS6和更高版本的上一个答案(Objective-C仍然有效):

在iOS6 UILabel,UIButton,UITextView,UITextField,支持属性字符串,这意味着我们不需要创建CATextLayers作为我们的属性字符串的收件人。此外,为了使属性字符串,我们不需要再次使用COreText :)我们在obj -c Foundation.framework中有新类,像NSParagraphStyle和其他常量,这将使我们的生活更轻松。好极了!

所以,如果我们有这个字符串:

NSString *text = @"updated: 2012/10/14 21:59"

我们只需要创建属性字符串:

if ([_label respondsToSELEctor:@SELEctor(setAttributedText:)])
{
    // iOS6 and above : Use NSAttributedStrings

    // Create the attributes
    const cGFloat fontSize = 13;
    NSDictionary *attrs = @{
        NSFontAttributename:[UIFont boldSystemFontOfSize:fontSize],NSForegroundColorAttributename:[UIColor whiteColor]
    };
    NSDictionary *subAttrs = @{
        NSFontAttributename:[UIFont systemFontOfSize:fontSize]
    };

    // Range of " 2012/10/14 " is (8,12). Ideally it shouldn't be hardcoded
    // This example is about attributed Strings in one label
    // not about internationalisation,so we keep it simple :)
    // For internationalisation example see above code in swift
    const NSRange range = NsmakeRange(8,12);

    // Create the attributed String (text + attributes)
    NSMutableAttributedString *attributedText =
      [[NSMutableAttributedString alloc] initWithString:text
                                             attributes:attrs];
    [attributedText setAttributes:subAttrs range:range];

    // Set it in our UILabel and we are done!
    [_label setAttributedText:attributedText];
} else {
    // iOS5 and below
    // Here we have some options too. The first one is to do something
    // less fancy and show it just as plain text without attributes.
    // The second is to use CoreText and get similar results with a bit
    // more of code. Interested people please look down the old answer.

    // Now I am just being lazy so :p
    [_label setText:text];
}

有一些好的介绍性的博客文章here从家伙在invasivecode,解释与NSAttributedString更多的例子使用,寻找“介绍NSAttributedString为iOS 6”和“使用界面生成器的iOS的属性字符串”:)

PS:上面的代码它应该工作,但它是脑编译。我希望它是足够:)

iOS5及以下的旧答案

使用带有NSAttributedString的CATextLayer!比2 UILabels轻得多和简单。 (iOS 3.2及更高版本)

例。

不要忘记添加QuartzCore框架(需要CALayers)和CoreText(需要属性字符串)。

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>

下面的示例将添加一个子图层到导航控制器的工具栏。 àla Mail.app在iPhone。

本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的iphone – 在单个UILabel中的粗体和非粗体文本?全部内容,希望文章能够帮你解决iphone – 在单个UILabel中的粗体和非粗体文本?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。