- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我读了this和 this但他们似乎没有解决这个问题。以及 this 的答案似乎在 NSCoding
和 friend 中离题了,所以 ...
考虑:
- (NSAttributedString *) attributedStringForText: (NSString *) text {
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:text];
UIFont * bigF = [UIFont fontWithName:@"OpenSans-Extrabold" size:20.0f] ;
UIFont * smaF = [UIFont fontWithName:@"OpenSans-Extrabold" size:12.0f] ;
[attrStr addAttribute:NSFontAttributeName value:bigF range:(NSRange){0, 3}] ;
[attrStr addAttribute:NSFontAttributeName value:smaF range:(NSRange){3, 6}] ;
[attrStr addAttribute:NSFontAttributeName value:bigF range:(NSRange){6, [text length]-6}] ;
[attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor brownColor] range:(NSRange){3, 6}] ;
return attrStr ;
}
- (CALayer *) stealLayerFromUILabelForText: (NSString *) text inRect: (CGRect) bounds {
UILabel * label = [[UILabel alloc] initWithFrame:bounds] ;
label.textColor = [UIColor whiteColor] ;
label.font = [UIFont fontWithName:@"OpenSans-Extrabold" size:20.0f] ;
label.attributedText = [self attributedStringForText:text] ;
[label sizeToFit] ;
[label.layer display] ; // Yup!
return label.layer ;
}
和
- (void) setupLayer: (CALayer *) tileLayer
text: (NSString *) text
index: (NSUInteger) index {
CGRect (^center_rect)(CGRect, CGRect) = ^CGRect (CGRect r, CGRect into) {
CGRect centered = into ;
centered.origin.x += (into.size.width - r.size.width) / 2.0f ;
centered.origin.y += (into.size.height - r.size.height) / 2.0f ;
centered.size = r.size ;
return centered ;
} ;
CALayer * layer = [self stealLayerFromUILabelForText:text inRect:tileLayer.bounds] ;
CGRect frame = center_rect(layer.bounds, tileLayer.bounds) ;
[tileLayer addSublayer:layer] ;
layer.frame = frame ;
}
这个有效:
但我担心我会以某种方式滥用 UILabel 和 CALayer。一方面,UIView
、UILabel
和所有这些都只是在真实 CALayer
层次结构。 OTOH 我在某种程度上依赖于许多甚至没有在这里陈述的假设。
任何人都明白为什么这会在未来进一步崩溃,或者更好的是,可以证明它不会?
最佳答案
我从 2013 年开始使用这种技术,它仍然很稳定。所以是的,我认为现在这是安全的。以前我这样做时没有在 iOS 4 或 5 上调用 -display,这在针对较新的 SDK 编译时不再有效,所以感谢这个花絮。
如果您正在寻找其他选项,这里有更多信息:Add text to CALayer
关于ios - 将 "stolen"UILabel 层添加到其他 CALayer 的子层是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23171790/
我有一个类似守护进程的小型应用程序,它在特定端口上监听,但碰巧不同的应用程序使用相同的端口,以某种方式将其从我的应用程序中夺走。 在启动其他应用程序之前一切正常,一旦我启动它我就不再获得连接,但是在关
这就是我遇到的问题, 我打开了我的工作副本,进行了更新,这花费了大量时间(我中断了它),然后清理了锁,然后出现了这个错误: Write lock stolen 我真的不知道这里发生了什么,我读了一些有
我在浏览器中执行了以下 javascript 代码: function run(request) { var res; $.ajax({ url:'http://custo
数据库设计: fiddle :http://sqlfiddle.com/#!2/4f23b3 我有一张表 players CREATE TABLE players ( id MEDIUMINT
我读了this和 this但他们似乎没有解决这个问题。以及 this 的答案似乎在 NSCoding 和 friend 中离题了,所以 ... 考虑: - (NSAttributedString *)
我是一名优秀的程序员,十分优秀!