我怎么能有UILabel两种不同颜色的字体?我将在两个不同的字符串中有文本,我想制作第一个字符串为红色,第二个为绿色的文本.两个字符串的长度都是可变的.
因此,对于iOS 6,UITextView可以使用一个referencedString,这对于语法高亮显示非常有用.
我正在做一些正则表达式模式,-textView:shouldChangeTextInRange:replacementText:我经常需要更改已经输入的单词的颜色.我认为没有其他选择,只能重置attributesText,这需要时间.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//A context will allow us to not call -attributedText on the textView, which is slow.
//Keep context up to date
[self.context replaceCharactersInRange:range withAttributedString:[[NSAttributedString alloc] initWithString:text attributes:self.textView.typingAttributes]];
// […]
self.textView.scrollEnabled = FALSE;
[self.context setAttributes:self.defaultStyle range:NSMakeRange(0, self.context.length)];
[self refresh]; //Runs regex-patterns in the context
textView.attributedText = self.context;
self.textView.selectedRange = NSMakeRange(range.location + text.length, 0);
self.textView.scrollEnabled = TRUE;
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
这在模拟器上运行得很好,但在iPad 3上每个都-setAttributedText需要几百毫秒.
我向Apple提交了一个错误,要求能够改变attributionText.它被标记为重复,所以我看不出他们对此有何看法.
更具体的问题:
如何在UITextView中更改某些范围的颜色,使用大量的彩色文本,并且每个都有足够的性能shouldReplaceText...? …
syntax-highlighting uitextview nsattributedstring uitextviewdelegate ios6
我正在寻找一种改变颜色的方法UIRefreshControl.文本显示在一个NSAttributedString,所以我尝试使用CoreText.framework:
NSString *s = @"Hello";
NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:s];
[a addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSRangeFromString(s)];
refreshControl.attributedTitle = a;
Run Code Online (Sandbox Code Playgroud)
文本显示正确,但颜色始终为默认灰色.有任何想法吗 ?
我正在将UITextView与textContainer属性和属性字符串一起使用...
UITextView * textView = [[UITextView分配] initWithFrame:textViewFrame textContainer:textContainer];
但是,此文本视图变得无法选择。
我试图子类的UITextView并返回YES到canBecomeFirstResponder方法,但它仍然不可选择..
如何解决这个问题?
这是我的代码的一部分
NSString * decodedHtmlString = [[contentString stringByDecodingHTMLEntities] stringWithNewLinesAsBRs];
NSString * plainText = [decodedHtmlString stringByConvertingHTMLToPlainText];
NSData * dataString = [decodedHtmlString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSDictionary * attributedOptions = [NSDictionary dictionaryWithObjectsAndKeys:
NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute
, nil];
NSAttributedString * attributesString = [[NSAttributedString alloc] initWithData:dataString
options:attributedOptions
documentAttributes:nil
error:nil];
/////direction
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;
NSMutableAttributedString * mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:attributesString];
[mutableAttrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" …Run Code Online (Sandbox Code Playgroud) 是否有一种允许在本机iOS应用程序中编辑HTML内容的好方法?
例如,如果您要构建电子邮件应用程序并希望允许回复电子邮件并编辑原始邮件.它需要支持任何有效的HTML,包括图像,样式等.
我尝试了一个contenteditable UIWebView以及使用NSAttributedStringa UITextView,但两个都不是好的解决方案 - UIWebView没有任何方法来管理内容或光标位置,并且UITextView不会呈现包含图像和其他元素的HTML.
我试图改变NSAttributedString中的写入方向.但是,我很难搞清楚如何去做.
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"ArialRoundedMTBold", 16, NULL);
NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,(NSString *)kCTFontAttributeName, nil];
CFRelease(fontRef);
NSAttributedString *attString=[[NSAttributedString alloc] initWithString:self.stringMap attributes:attrDictionary];
self.attString = attString;
Run Code Online (Sandbox Code Playgroud)
这是我初始化我的NSAttributedString的代码,我已经阅读了关于常量kCTWritingDirectionRightToLeft的内容,我觉得我必须把它放在某处,但我可以弄清楚在哪里和如何.
有人有什么建议吗?
我想转换NSString成NSAttributedString.但我总是要这样做
NSAttributedString *useDict1=[[NSAttributedString alloc] initWithString:@"String"];
Run Code Online (Sandbox Code Playgroud)
有没有其他方式,我不必Dictionary每次都分配,但只是给字符串?
我有一个UITableView,我想在同一行中使用不同的颜色显示每一行的文本.
我试过这个代码,尝试从Obj-C翻译,但我不能让它工作
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description)
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length))
var stringToCell:String = String(format: "%@ %@", attrString, object.valueForKey("example2")!.description)
cell.textLabel?.text = stringToCell
Run Code Online (Sandbox Code Playgroud)
这一切的输出是

数字34对应的地方object.valueForKey("example1")!.description,问题是数字不是红色,第二部分(object.valueForKey("example2")!.description)替换为{.
如果我留下关于NSAttributedString行文本的这段代码正确显示.
我有一个像这样的NSAttributedString:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"];
NSInteger length = str.length;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)];
Run Code Online (Sandbox Code Playgroud)
NSMutableAttributedString设置为UILabel,如下所示:
label.attributedText = str;
Run Code Online (Sandbox Code Playgroud)
如何在另一个视图控制器中为上面的字符串中的'@clickhere'做出一个轻击手势(或可点击的东西)?
谢谢!
iphone nsattributedstring uilabel ios uitapgesturerecognizer
让我们想象一下我有一些文字:" 有些文字是梯形的. "
我有NSAttributedString扩展,它返回带有属性文本的UIImage:
extension NSAttributedString {
func asImage() -> UIImage? {
defer {
UIGraphicsEndImageContext()
}
let size = boundingRect(with: CGSize.zero, options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], context: nil).size
UIGraphicsBeginImageContext(size)
draw(at: CGPoint.zero)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个函数在一行中返回文本,因为使用boundingRect:
------------------------------------
|Some attributed text in trapezoid.|
------------------------------------
Run Code Online (Sandbox Code Playgroud)
如果我使用自定义rect来绘制文本,那将无济于事......
UIGraphicsBeginImageContext(CGRect(x: 0, y: 0, width: 100, height: 30))
draw(at: CGPoint.zero)
Run Code Online (Sandbox Code Playgroud)
...因为文字将是矩形:
--------------
|Some attribu|
|ted text in |
|trapezoid. |
--------------
Run Code Online (Sandbox Code Playgroud)
我需要的是在具有已知角位置(或具有已知半径的圆圈)的梯形中绘制文本.所以每个新的文本行都应该以一点偏移量开始,参见示例:

所以我希望看到类似的东西:
---------------
\Some attribut/
\ed text in /
\trapezoid/
---------
Run Code Online (Sandbox Code Playgroud)
我怎样才能达到这个效果?