Cok*_*kes 2 macos cocoa objective-c nsattributedstring
我正在尝试为我的应用添加一些不同的文字颜色以融合到图像中.我有很多输入,我的用户想要彩虹文字颜色和重复.例如,单词:stackoverflow看起来像这样:s=red t=orange a=yellow c=green k=blue o=purple v=pink e=red r=orange f=yellow l=green o=blue w=purple
我甚至无法开始思考如何在一个单一的环境中做到这一点UITextView
有人知道如何在用户输入时实现这一目标吗?提示?例?
关于iOS的彩虹文字,我没有看到关于SO的任何其他帖子.(如我错了请纠正我)
你可以使用NSAttributedString:
使其成为支持OSX和iOS的通用方法.现在没有必要改变NSColor对UIColor,用这个两个操作系统.
#if TARGET_OS_IPHONE
typedef UIColor Color;
#elif TARGET_OS_MAC
typedef NSColor Color;
#endif
-(NSAttributedString *)colorfulStringFrom:(NSString *)string{
NSArray *colors = @[[Color redColor],
[Color yellowColor],
[Color greenColor],
[Color blueColor],
[Color purpleColor],
[Color magentaColor]
];
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string];
for (NSInteger location=0; location<string.length; location++) {
NSRange range = NSMakeRange(location, 1);
Color *color = colors[location%colors.count];
[attribString addAttribute:NSForegroundColorAttributeName value:color range:range];
}
return attribString;
}
Run Code Online (Sandbox Code Playgroud)
输出:

| 归档时间: |
|
| 查看次数: |
786 次 |
| 最近记录: |