我想实现高级搜索功能,以便在搜索栏中输入特定文本时,应根据搜索过滤UITableview中的内容列表,然后突出显示搜索文本?请给我一些想法?
我有UILabel两个由新行分隔的属性字符串.第一个字符串的字体大小设置为17,第二个字符串设置为14. NSMutableAttributedString如果内容不能放在一行中,我希望我的第一个字符串调整为最小字体大小.
那可能吗?
UILabel通过在IB中为纯文本设置"自动缩小到最小字体大小" 来配置此类行为非常简单,但不知道如何为属性文本执行此操作.
这是我的代码:
NSString *eventName = @"Looong Event Name";
NSString *placeString = @"My place";
eventName = [eventName stringByAppendingString:@"\n"];
NSMutableAttributedString *attrName = [[NSMutableAttributedString alloc] initWithString:eventName];
[attrName addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, [eventName length])];
NSMutableAttributedString *attrPlace = [[NSMutableAttributedString alloc] initWithString:placeString];
[attrPlace addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeString.length)];
[attrPlace addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, placeString.length)];
NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:attrName];
[finalString appendAttributedString:attrPlace];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.attributedText = finalString;
Run Code Online (Sandbox Code Playgroud) 我在iOS应用程序上应该能够突出显示文本,并使其可以点击.
我在iOS中读到了关于NSAttributedString但它仍然比android中的Spannable更复杂.
有没有其他Objective c方法可以做到这一点,如果没有; 我该怎么做NSAttributedString才能逐字逐句地突出显示一个段落,以及如何使我的文字可以点击.
更新:
我究竟想要的是每个单词都应该是可点击的,并且可以 在一个段落中突出显示为单个单词.
objective-c nsattributedstring core-text ios spannablestring
我的App.在很多地方我都使用过System Bold Font,它已经成为我应用程序的标准配置.
如果我使用Attributed String默认显示的字体是"Helvetica Neue".在所有字体中,我搜索了系统粗体字体,但找不到它.
那么,在使用归属字符串时如何设置系统粗体字体?

我有一个NSTextView.我将图像粘贴到其中并查看.当我获得文本视图的NSAttributedString的NSTextAttachment时,它的文件包装器是nil.如何获取粘贴到文本视图中的图像数据?
我在NSAttributedString上使用类别来获取文本附件.如果可能的话,我宁愿不写入磁盘.
- (NSArray *)allAttachments
{
NSError *error = NULL;
NSMutableArray *theAttachments = [NSMutableArray array];
NSRange theStringRange = NSMakeRange(0, [self length]);
if (theStringRange.length > 0)
{
NSUInteger N = 0;
do
{
NSRange theEffectiveRange;
NSDictionary *theAttributes = [self attributesAtIndex:N longestEffectiveRange:&theEffectiveRange inRange:theStringRange];
NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
if (theAttachment != NULL){
NSLog(@"filewrapper: %@", theAttachment.fileWrapper);
[theAttachments addObject:theAttachment];
}
N = theEffectiveRange.location + theEffectiveRange.length;
}
while (N < theStringRange.length);
}
return(theAttachments);
}
Run Code Online (Sandbox Code Playgroud) 我有一个UILabel显示NSAttributedString.该字符串包含文本和UIImagea NSTextAttachment.
在呈现时,有没有一种方式来获得的位置NSTextAttachment的UILabel?
编辑
这是我想要达到的最终结果.
当文本只有1行长时,图像应该在正确的边缘UILabel.简单:

如果您有多行,但仍希望图像位于最后一行的末尾,则会出现问题:

我写了一个简单的扩展来解码html实体:
extension String {
func htmlDecode() -> String {
if let encodedData = self.data(using: String.Encoding.unicode) {
let attributedString = try! NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.unicode], documentAttributes: nil)
return attributedString.string
}
return self
}
}
Run Code Online (Sandbox Code Playgroud)
现在它在行上抛出一个错误if let attributedString …:
***由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:索引4超出边界[0 .. 2]'
而self不是零什么的,只是一个String像这样的:
self = (String) "...über 25'000 Franken..."
这种奇怪的NSArray例外来自哪里?
我无法NSAttributedString在视图中绘制一个没有边距的东西.这是我的代码:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:72.0f]};
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Hello"
attributes:attributes];
[string drawWithRect:rect
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics
context:nil];
Run Code Online (Sandbox Code Playgroud)
这会导致以下行为:

请注意左侧和顶部的边距.
我意识到可能有一些方法来对齐文本而不使其符合视图的边缘,但是使其符合视图的边缘将允许我使用自动布局等直观地使用视图.
当字符串包含前导或尾随空格时,我不关心行为.
如果无法做到这一点NSAttributedString,是否有其他方法可以获得您建议的此行为?
澄清一下,这是我想要的第一号.

我正在尝试确定a中给定字符串出现的索引String,然后NSRange使用这些索引生成一个以便为a添加属性NSMutableAttributedString.问题是rangeOfString退货,Range<Index>但addAttributes:range:预计会有NSRange.我尝试NSRange从开始和结束索引创建Range失败,因为String.CharacterView.Index不是Int,因此它不会编译.
如何使用Range<Index>值来创建NSRange?
var originalString = "Hello {world} and those who inhabit it."
let firstBraceIndex = originalString.rangeOfString("{") //Range<Index>
let firstClosingBraceIndex = originalString.rangeOfString("}")
let range = NSMakeRange(firstBraceIndex.startIndex, firstClosingBraceIndex.endIndex)
//compile time error: cannot convert value of type Index to expected argument type Int
let attributedString = NSMutableAttributedString(string: originalString)
attributedString.addAttributes([NSFontAttributeName: boldFont], range: range)
Run Code Online (Sandbox Code Playgroud) 我有像这样的"@Mervin测试员"的表情符号的AttributedString
现在我需要在这个属性String中找到一系列Mervin.
let attributedString = NSMutableAttributedString(string: " @Mervin tester ")
let range = // range for "Mervin" in above String.
Run Code Online (Sandbox Code Playgroud)
谢谢.