首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAttributedString on UIButton

NSAttributedString on UIButton
EN

Stack Overflow用户
提问于 2017-08-01 07:19:21
回答 1查看 2K关注 0票数 0
代码语言:javascript
复制
let strNo = "2222555" // size 18, this should be bold

let remainingStr = "Call to" + "\(strNo)" + "Number" 

现在,在我的UIButton中,比如说按钮,如何设置这个标题“调用2222555号码”,我需要根据设备更改大小,所以我必须通过编码来实现。

更新

我需要像下面这样的图像。

并且需要通过编码来改变标题的大小。上面的屏幕截图来自iPhone 7,在iPhone 5中它变得更大,在iPad中它变得更小,所以我必须根据需要设置标题的大小。

任何帮助都是有价值的。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-04 07:07:21

我通过几天的努力找到了答案。而且这很容易。

代码语言:javascript
复制
guard
            let font1 = UIFont(name: "HelveticaNeue-Bold", size: 22),
            let font2 = UIFont(name: "HelveticaNeue-Medium", size: 22)  else { return }

        let dict1:[String:Any] = [
            //            NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
            NSFontAttributeName:font2,
            NSParagraphStyleAttributeName:style,
            NSForegroundColorAttributeName: UIColor.white
        ]

        let dict2:[String:Any] = [
            //            NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
            NSFontAttributeName:font1,
            NSParagraphStyleAttributeName:style,
            NSForegroundColorAttributeName: UIColor.white,
            //            NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)
        ]

        let dict3:[String:Any] = [
            //            NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
            NSFontAttributeName:font2,
            NSParagraphStyleAttributeName:style,
            NSForegroundColorAttributeName: UIColor.white
        ]

        let dict4:[String:Any] = [
            //            NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
            NSFontAttributeName:font1,
            NSParagraphStyleAttributeName:style,
            NSForegroundColorAttributeName: UIColor.white
        ]

        let attString = NSMutableAttributedString()
        attString.append(NSAttributedString(string: "Call to", attributes: dict1))
        attString.append(NSAttributedString(string: " 2222555 ", attributes: dict2))
        attString.append(NSAttributedString(string: " To Book Your Skip ", attributes: dict3))
        attString.append(NSAttributedString(string: "NOW", attributes: dict4))

        btnCall.setAttributedTitle(attString, for: .normal)

和预期一样完美地运作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45431347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档