标签: nsattributedstring

在iphone中推进搜索实施?

我想实现高级搜索功能,以便在搜索栏中输入特定文本时,应根据搜索过滤UITableview中的内容列表,然后突出显示搜索文本?请给我一些想法?

iphone uitableview nsattributedstring

9
推荐指数
1
解决办法
2152
查看次数

iOS - 使用归属文本自动缩小UILabel

我有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)

iphone xcode cocoa-touch nsattributedstring ios

9
推荐指数
1
解决办法
7840
查看次数

什么是Objective-C中Android的"Spannable"

我在iOS应用程序上应该能够突出显示文本,并使其可以点击.

我在iOS中读到了关于NSAttributedString但它仍然比android中的Spannable更复杂.

有没有其他Objective c方法可以做到这一点,如果没有; 我该怎么做NSAttributedString才能逐字逐句地突出显示一个段落,以及如何使我的文字可以点击.

更新:

我究竟想要的是每个单词都应该是可点击的,并且可以 在一个段落中突出显示为单个单词.

objective-c nsattributedstring core-text ios spannablestring

9
推荐指数
1
解决办法
6507
查看次数

找不到属性字符串iOS Xcode的系统粗体字体

我的App.在很多地方我都使用过System Bold Font,它已经成为我应用程序的标准配置.

如果我使用Attributed String默认显示的字体是"Helvetica Neue".在所有字体中,我搜索了系统粗体字体,但找不到它.

那么,在使用归属字符串时如何设置系统粗体字体?

在此输入图像描述

nsattributedstring ios xcode5

9
推荐指数
1
解决办法
5853
查看次数

如何从NSAttributedString获取图像数据

我有一个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)

cocoa nsattributedstring

9
推荐指数
1
解决办法
5484
查看次数

NSTextAttachment在UILabel中的位置?

我有一个UILabel显示NSAttributedString.该字符串包含文本和UIImagea NSTextAttachment.

在呈现时,有没有一种方式来获得的位置NSTextAttachmentUILabel

编辑

这是我想要达到的最终结果.

当文本只有1行长时,图像应该在正确的边缘UILabel.简单:

单行UILabel

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

多线UILabel

objective-c nsattributedstring uilabel nstextattachment ios

9
推荐指数
1
解决办法
3659
查看次数

NSAttributedString初始化抛出NSRangeException

我写了一个简单的扩展来解码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&#39;000 Franken..."

这种奇怪的NSArray例外来自哪里?

nsattributedstring ios swift

9
推荐指数
1
解决办法
1166
查看次数

如何在视图中绘制没有边距的字符串?

我无法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)

这会导致以下行为:

用边距绘制的字符串

请注意左侧和顶部的边距.

  1. 如何避免这些边距并使文本完全符合包含视图的边缘?我想要的是实际绘制的文本与视图的边缘相交,也就是说,在任何字形中绘制的最顶部像素应该在视图的顶部,并且在任何字形中绘制的最左边的像素位于视图的左侧.风景.
  2. 假设1是可能的,有没有办法可以得到绘制文本的实际宽度和高度,这样我就可以计算字体大小和字距以使文本符合底部和右边缘?

我意识到可能有一些方法来对齐文本而不使其符合视图的边缘,但是使其符合视图的边缘将允许我使用自动布局等直观地使用视图.

当字符串包含前导或尾随空格时,我不关心行为.

如果无法做到这一点NSAttributedString,是否有其他方法可以获得您建议的此行为?

澄清一下,这是我想要的第一号.

期望的行为

string uikit uiview nsattributedstring ios

9
推荐指数
2
解决办法
359
查看次数

在NSAttributedString API中使用Range <Index>和NSRange

我正在尝试确定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)

range nsattributedstring ios nsrange swift

9
推荐指数
1
解决办法
7842
查看次数

在NSMutableAttributedString中找到子串的Rang

我有像这样的"@Mervin测试员"的表情符号的AttributedString

现在我需要在这个属性String中找到一系列Mervin.

let attributedString = NSMutableAttributedString(string: " @Mervin tester ")

let range = // range for "Mervin" in above String. 
Run Code Online (Sandbox Code Playgroud)

谢谢.

nsattributedstring nsrange nsmutableattributedstring swift

9
推荐指数
1
解决办法
4118
查看次数