如何在iOS中的NSAttributedString中设置属性? [英] how to set attributes in NSAttributedString in iOS?

查看:213
本文介绍了如何在iOS中的NSAttributedString中设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS中设置NSAttributedString的臭名昭着的NSFontAttributeName属性,但它似乎不工作:


  1. 首先,没有为iOS定义的NS常量。

  2. 我在某处读到了,我可以通过传递
    的CoreText常量。好...但仍然,属性
    期望一个NSFont和我卡住UIFont或CTFontRef,
    似乎无效:

这不起作用:

  CTFontRef ctFont = CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:16] .fontName,[UIFont boldSystemFontOfSize:16] .pointSize,NULL); 
[myAttString addAttribute:(NSString *)kCTFontNameAttribute
value:(id)ctFont
range:NSMakeRange(0,myAttString.length-1)];

这不工作:

  [myAttString addAttribute:(NSString *)kCTFontNameAttribute 
value:[UIFont boldSystemFontOfSize:16]
range:NSMakeRange(0,myAttString.length-1)];

有没有要做这项工作?



基本上,结果是我应该使用的字典键的字符串常量是kCTFontAttributeName

p>

这整件事都是一个表演...


I'm trying to set the infamous NSFontAttributeName property of an NSAttributedString in iOS but it just doesn't seem to work:

  1. first off, none of the NS constants seem defined for iOS
  2. I read somewhere that I could instead work around it by passing the CoreText consts instead. Fine... but still, The attribute expects an NSFont and I'm stuck with UIFont or CTFontRef, neither of which seems to work:

this doesn't work:

CTFontRef ctFont = CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:16].fontName, [UIFont boldSystemFontOfSize:16].pointSize, NULL);
[myAttString addAttribute:(NSString*)kCTFontNameAttribute
                          value:(id)ctFont
                          range:NSMakeRange(0, myAttString.length-1)];

this doesn't work:

[myAttString addAttribute:(NSString*)kCTFontNameAttribute
                          value:[UIFont boldSystemFontOfSize:16]
                          range:NSMakeRange(0, myAttString.length-1)];

Is there anyway to make this work?

解决方案

I found it!

basically, turns out the string constant for the dictionary key I should been using is kCTFontAttributeName

This whole thing is a show...

这篇关于如何在iOS中的NSAttributedString中设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆