我需要UILabel在iOS7中嵌入小图标(一些自定义项目符号).我如何在界面设计器中执行此操作?或者至少在代码中?
在Android中有leftDrawable和rightDrawable贴标签,但它是如何在iOS中做了什么?android中的示例:

我有一个显示NSAttributedString的UITextView.这个字符串包含我想要点击的单词,这样当它们被点击时我会被回叫以便我可以执行一个动作.我意识到UITextView可以检测URL上的点击并回调我的委托,但这些不是URL.
在我看来,iOS7和TextKit的强大功能现在应该可行,但我找不到任何示例,我不知道从哪里开始.
我知道现在可以在字符串中创建自定义属性(虽然我还没有这样做),也许这些对于检测是否有一个神奇的单词被点击是有用的?在任何情况下,我仍然不知道如何拦截该敲击并检测敲击发生在哪个单词上.
请注意,不需要iOS 6兼容性.
我试图使用UITextView获得以下效果:

基本上我想在文本之间插入图像.图像可以简单地占用1行空间,因此不需要包装.
我尝试在子视图中添加一个UIView:
UIView *pictureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[pictureView setBackgroundColor:[UIColor redColor]];
[self.textView addSubview:pictureView];
Run Code Online (Sandbox Code Playgroud)
但它似乎漂浮在文本上并覆盖它.
我在排除路径上做了一些阅读,这似乎是实现这一点的一种方式.但是,我不想绝对定位图像 - 相反,它应该与文本一起流动(类似于<span>HTML中的行为).
对于给定的NSRange,我想找到一个CGRect在UILabel对应的的字形NSRange.例如,我想CGRect在"快速的棕色狐狸跳过懒狗"这句话中找到包含"狗"字样的字样.

诀窍是,UILabel有多行,而文本确实attributedText如此,所以找到字符串的确切位置有点困难.
我想在我的UILabel子类上写的方法看起来像这样:
- (CGRect)rectForSubstringWithRange:(NSRange)range;
Run Code Online (Sandbox Code Playgroud)
细节,对于有兴趣的人:
我的目标是能够创建一个具有UILabel的确切外观和位置的新UILabel,然后我可以制作动画.我已经把剩下的事情搞清楚了,但特别是这一步让我暂时退缩了.
到目前为止我尝试解决问题的方法是:
UITextView和UITextField而不是UILabel.我敢打赌,对此的正确答案涉及以下其中一项:
NSLayoutManager和textContainerForGlyphAtIndex:effectiveRange更新:这是一个github要点,我已经尝试了迄今为止解决这个问题的三件事:https://gist.github.com/bryanjclark/7036101
在iOS 7中,当使用新的从屏幕边缘轻扫手势向后导航时,后退按钮("艺术家")的标题从粉红色(在下面的示例中)淡化并且具有常规字体粗细为黑色并具有粗体字体重.

在我看来,动画使用两个不同的标签,以实现这种效果; 随着另一个淡入淡出,一个淡出.然而,Apple已经以某种方式调整了字体,使常规标签完美地覆盖了粗体,从而产生了在两种不同重量和颜色之间变形的单个标签的错觉.
他们是否只是调整了常规字体上的字母间距,使其与粗体字符匹配?在那种情况下,如何在iOS 7中实现?Text Kit是否有任何令人敬畏的功能,或者我应该怎么做?
我的textViews的格式在iOS 6中运行良好,但不再在iOS 7中运行.我理解使用Text Kit的内容已经发生了很大变化.它变得非常令人困惑,我希望有人可以通过帮助我做一些简单的事来帮助理顺它.
我的静态UITextView最初被分配了一个值textColor和textAlignment属性.然后我做了一个NSMutableAttributedString,为它分配了一个属性,然后将它分配给textView的attributedText属性.对齐和颜色在iOS 7中不再生效.
我怎样才能解决这个问题?如果这些属性不起作用,为什么它们不再存在?这是textView的创建:
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
[self.view addSubview:titleView];
Run Code Online (Sandbox Code Playgroud) 对于a UILabel,我想知道从触摸事件接收的特定点处的哪个字符索引.我想使用Text Kit为iOS 7解决这个问题.
由于UILabel不提供对它的访问NSLayoutManager,我根据这样UILabel的配置创建了自己的:
- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint location = [recognizer locationInView:self];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedText];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
[layoutManager addTextContainer:textContainer];
textContainer.maximumNumberOfLines = self.numberOfLines;
textContainer.lineBreakMode = self.lineBreakMode;
NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textStorage.length) {
NSRange range = NSMakeRange(characterIndex, 1);
NSString *value = [self.text substringWithRange:range];
NSLog(@"%@, %zd, %zd", value, range.location, range.length); …Run Code Online (Sandbox Code Playgroud) 我有一个NSAttributed字符串(来自HTML)我设置为UITextView.
- (void)setHtml:(NSString *)html {
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
// Create the HTML string
NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSError *error = nil;
self.htmlString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];
self.editorView.attributedText = self.htmlString;
}
Run Code Online (Sandbox Code Playgroud)
然后我让用户编辑他们想要的内容,然后我想再将其转换为HTML,所以我使用:
- (NSString *)getHTML {
NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [self.editorView.attributedText dataFromRange:NSMakeRange(0, self.editorView.attributedText.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}
Run Code Online (Sandbox Code Playgroud)
它确实返回HTML,但它不是我想要的.一切都被赋予了一个类属性,以及它放在文档顶部的CSS.图像和链接之类的东西甚至不包含在返回的HTML中,可能还有更多问题.
有没有更好的方法来获取HTML NSAttributedString?或者,有没有办法解析NSAttributedString并编写自己的HTML?
我可以NSAttributedString使用转义的换行符(@"\n")创建一个多行.使用iOS 7,我现在可以嵌入一个UIImage内部属性字符串(via NSTextAttachment).
我注意到每当我将attributedTexta 设置为UILabel带有嵌入图像的多行属性字符串时,实际显示的行数与标签的高度成反比.例如,当标签的高度为80时,会出现两条线; 当高度大约为100时,只出现第二条线; 当高度约为130时,什么都没有出现.
尝试将多个UILabel并排放置在a UITableViewCell内并使标签与单元格高度(垂直)增长时发生此问题.
任何人都可以解释为什么会这样吗?有没有人知道不涉及使UILabel变小的解决方法?
示例代码:
@implementation SOViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] init];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];
UIImage *image = [UIImage imageNamed:@"17x10"]; //some PNG image (17px by 10px)
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
NSMutableAttributedString *text2 = [[NSMutableAttributedString …Run Code Online (Sandbox Code Playgroud) 我正在为iOS 7编写一个应用程序,我正在尝试在不可编辑的UITextView中获得不错的格式.
插入一个子弹点角色很容易,但当然左压痕不会跟随.iOS 7在弹出点后设置左缩进的最简单方法是什么?
提前致谢,
坦率