【问题标题】:Change color a part text of sentence in iOS [duplicate]在iOS中更改句子的部分文本[重复]
【发布时间】:2012-12-06 06:31:35
【问题描述】:

可能重复:
TTT attributed Label Multi- colored Font help

我怎样才能在 iphone 中像这张照片一样?任何人都可以帮助我。我应该使用 Core Animation 或 Text Core 还是其他东西? http://s7.postimage.org/qgbhw7c3v/Capture.png

【问题讨论】:

标签: iphone ios xcode


【解决方案1】:
    NSString *test = @"How can i Do that? (just only one string and don't devided into 2 part)";

    CFStringRef string =  (CFStringRef) test;
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string);



    /*
     Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its
     attributes right when we create it. We can change them if we use mutable form of CFAttributeString.
     */




    CGColorRef _red=[UIColor redColor].CGColor;

    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 11),kCTForegroundColorAttributeName, _red);    

注意 => 在项目中添加CoreText框架并导入。

【讨论】:

  • @hackchick 还可以查看本教程以了解字符串中的多种颜色。soulwithmobiletechnology.blogspot.in/2011/06/…
  • 我想改变句子中字母的每个像素,例如字母“A”,我只想改变字母“A”的1/2部分的红色,我该怎么做那个?
  • 这里如果你想为每个字母着色,那么你可以使用这个逻辑,伙计。只需使用这个 [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];尝试它只是实现它并更改一些代码
  • 还可以从我的这个答案stackoverflow.com/questions/13579209/… 中查看讨论,如果你喜欢它,请使用这个答案并为答案投票。我希望你明白了.. :)
  • 是的,我的想法是构建一个基本的卡拉 OK 应用程序。歌词会显示每个字母的像素并且颜色会改变,就像这个youtube.com/watch?v=MZxvu2sXzug,但我不知道我应该使用Core Animation,Core Text还是Quazt 2D,这对我来说很难,我是Xcode的新手我来自越南,所以翻译用英文写的文件对我来说可能很难:(
【解决方案2】:

您可以使用 OHAtributedLabel 来实现这一简单而有效的操作: https://github.com/AliSoftware/OHAttributedLabel

【讨论】:

  • 感谢您的回答,但我想更改句子中字母的每个像素,例如字母“A”,我只想将字母“A”的 1/2 部分更改为红色",我该怎么做?
猜你喜欢
  • 1970-01-01
  • 2015-12-12
  • 2015-04-20
  • 2014-06-21
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多