相关疑难解决方法(0)

NSAttributedString背景颜色和圆角

我有一个关于自定义圆角和文本背景颜色的问题UIView.

基本上,我需要在自定义UIView中实现这样的效果(图像附加 - 注意一侧的圆角): 背景亮点

我在考虑使用的方法是:

  • 使用Core Text获取字形运行.
  • 检查高光范围.
  • 如果当前运行在高亮范围内,则在绘制字形运行之前绘制具有圆角和所需填充颜色的背景矩形.
  • 绘制字形运行.

但是,我不确定这是否是唯一的解决方案(或者就此而言,这是否是最有效的解决方案).

使用a UIWebView不是一个选项,所以我必须在自定义中执行UIView.

我的问题是,这是最好的使用方法,我是否走在正确的轨道上?或者我错过了重要的事情或错误的方式?

objective-c quartz-graphics uiview nsattributedstring ios

50
推荐指数
4
解决办法
2万
查看次数

如何以UILabel缩小字体大小制作文本

如果UILabel包含太多文本,我如何设置我的标签以缩小字体大小?

以下是我设置UILabel的方法:

     descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
    [descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
    [descriptionLabel setBackgroundColor:[UIColor clearColor]];
    [descriptionLabel setTextColor:[UIColor whiteColor]];
    descriptionLabel.numberOfLines = 1;
    [self addSubview:descriptionLabel];
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c interface-builder

27
推荐指数
3
解决办法
2万
查看次数

CTLine具有"字符串访问权"是什么意思?

我正试图用UILabel来解决一个毛茸茸的问题,除了一件事之外,我已经弄清楚了它的大部分内容:我正在挑战了解对于CTLine"字符串访问" 意味着什么.

我想用的方法是CTLineGetOffsetForStringIndex.这是该方法文档的链接.

这是我不理解的文档部分(重点是我的):

charIndex沿基线的主偏移量,如果该行不支持字符串访问,则为0.0.

当我运行这个方法时,我0.0回来了,所以我猜这意味着该行不支持字符串访问 - 但这究竟是什么意思呢?

iphone uilabel core-text ios ctlineref

26
推荐指数
1
解决办法
571
查看次数

你如何抚摸NSAttributedString的_outside_?

我一直在使用NSStrokeWidthAttributeNameNSAttributedString对象,因为它是画把大纲文本周围.问题是笔划位于文本的填充区域内.当文本很小(例如1像素厚)时,抚摸使文本难以阅读.我真正想要的是外面的中风.有没有办法做到这一点?

我试过NSShadow没有偏移和模糊,但它太模糊,很难看到.如果有办法增加阴影的大小而没有任何模糊,那也可以.

cocoa objective-c nsattributedstring

13
推荐指数
1
解决办法
9328
查看次数

如何在iOS 7.0或更高版本中获得自动调整的字体大小?

我希望在UILabel或UITextField中缩小某些文本的字体大小.这在iOS 7.0之前是可能的:如何获得UILabel(UITextView)自动调整的字体大小?.但是,sizeWithFont已在iOS 7.0中弃用.我已经尝试使用它的替换,sizeWithAttributes,但没有成功.在iOS 7.0中有没有办法做到这一点?

uikit ios

11
推荐指数
3
解决办法
3708
查看次数

如何在ios中限制UITextView中的内容

我想在不同视图的TextViews中加载长文本.文本视图结束时,文本应分为页面.下一个textview必须从文本的继续开始.

我经历了很多答案.但是所有都指定了在输入数据时限制文本内容.我想在TextView中显示分页数据.并且不会有任何输入或编辑.因此,关于它的委托方法将不起作用.

我试着加载固定长度的文本.但是当段落的编号不同时,它是不正确的.

所以我想要找到的是,当文本到达textview的容量结束时获得通知,或者根据no lines/no.of字符获得textview的容量.有没有办法做到这一点?

更新

根据我的评论,我搜索了很多,并在NSTextStorage,NSTextLayoutManager和NSTextContainer达成

我发现这个链接有助于轻松实现分页

http://sketchytech.blogspot.co.uk/2013/11/paging-and-paginating-easy-way-with.html

在这个例子中,他们使用循环创建了4个对象.可以这么说,因为他们已经将字符串拆分为4个部分,并在滚动视图的4个uitextviews中显示.

我试图根据字符串的长度设置一个条件分割到textcontainers.我试图通过考虑主要字符串的总长度和textview中显示的文本来创建条件.

在此输入图像描述

但每个textview的文本长度与主字符串的总长度相同.那么我如何才能在textview的每个文本容器中显示长度文本?

pagination split uitextview ios

10
推荐指数
1
解决办法
2079
查看次数

layoutManager boundingRectForGlyphRange:inTextContainer:不适用于所有字符串

我有一个像字符串一样的推文的UILabel,包括其他用户的提及.

Hey @stephen and @frank and @Jason1.
Run Code Online (Sandbox Code Playgroud)

我试图让每个提及都可以点击,这样我就可以加载该用户的个人资料.我从另一个SO帖子中找到了一些代码(如何找到UGRabel中文本子字符串的CGRect?),我可以使用它来查找字符串中每个提及的位置.但是,它通常不适用于帖子中的最后一个或最后两个提及.

来自SO帖子的方法(稍加修改):

- (CGRect)boundingRectForCharacterRange:(NSRange)range
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.myLabel.attributedText];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.myLabel.bounds.size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];

    NSRange glyphRange;

    // Convert the range for glyphs.
    [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];

    return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
}
Run Code Online (Sandbox Code Playgroud)

然后,在touchesEnded:,我循环每次提及,获取主字符串中的范围,并检查触摸是否在CGRect内.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{        
    UITouch *touch = touches.allObjects[0];

    for (NSString *mention in self.mentions) { …
Run Code Online (Sandbox Code Playgroud)

objective-c ios

7
推荐指数
1
解决办法
2730
查看次数

NSLayoutManager boundingRectForGlyphRange由某些点关闭

我想在UITextView中"突出显示"特定的单词,而不是使用纯色(这可能是通过NSAttributedString),而是使用渐变和其他一些奇特的效果.

因此,我决定有必要使用给定文本的边界矩形手动创建视图并覆盖(或底层)它.

令我惊讶的是,事实证明这非常简单.实施例是一个UIViewController内,txtView被一个UITextView连接为IBOutlet:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Converting to NSString as I need a NSRange for glphyRangeForCharacterRange
    // (a normal String in Swift returns a Range)
    let charRange = (txtView.text as NSString).rangeOfString("dolor sit er elit")
    assert(charRange.location != NSNotFound, "Character Range could not be found.")

    let glyphRange = txtView.layoutManager.glyphRangeForCharacterRange(charRange,
        actualCharacterRange: nil)
    let glyphContainer = txtView.layoutManager.textContainerForGlyphAtIndex(glyphRange.location, effectiveRange: nil)
    let glyphRect = txtView.layoutManager.boundingRectForGlyphRange(glyphRange,
        inTextContainer: glyphContainer!)

    let highlightView = UIView(frame: glyphRect)
    highlightView.alpha = 0.3 …
Run Code Online (Sandbox Code Playgroud)

objective-c core-text ios textkit swift

6
推荐指数
1
解决办法
1318
查看次数

在UILabel中的draw#rect中获取字形boundingRect

使用Swift,我想在UILabel中的draw#rect中获得字形的boundingRect。

UILabel已经具有大小(在示例中为300x300)和诸如文本居中的质量。

class RNDLabel: UILabel {

    override func draw(_ rect: CGRect) {

        let manager = NSLayoutManager()

        let store = NSTextStorage(attributedString: NSAttributedString(
            string: text!,
            attributes: [NSAttributedString.Key.font: font]))
        store.addLayoutManager(manager)

        let textContainer = NSTextContainer(size: rect.size)
        // note, intrinsicContentSize is identical there, no difference
        manager.addTextContainer(textContainer)

        let glyphRange = manager.glyphRange(
           forCharacterRange: NSRange(location: 0, length: 1),
           actualCharacterRange: nil)
        let glyphRect = manager.boundingRect(
           forGlyphRange: glyphRange, in: textContainer)

        print("glyphRect \(glyphRect)")         
        ...context?.addRect(glyphRect), context?.drawPath(using: .stroke)

        super.draw(rect)
    }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

绿色方块不正确-应该更像这些红色方块!

在此处输入图片说明

似乎存在许多问题:

  • 当然,我制作的layoutManager应该获得UILabel的质量,例如“居中文本”吗?(不过,我相信您实际上不能直接访问UILabel的layoutManager?)

  • 我们应该使用像CTLineGetOffsetForStringIndex这样的东西吗?在draw#rect中甚至有可能吗

  • 注意,以及没有正确的偏移量,无论如何,绿色框似乎是错误的高度。(它看起来更像是一个普通的旧的internalContentSize,而不是字形边界框。)

如何?

作为记录,我的总体目标是根据实际字形框移动字形(当然,“ y”,“ X”等也不同)。但是总的来说,有很多有用的原因来了解字形框。

nslayoutmanager drawrect ios swift

5
推荐指数
1
解决办法
343
查看次数