Tag: nslayoutmanager

为什么我无法通过使用“usedRectForTextContainer”来获取textContainer的矩形

我在这个问题中学到了NAlexN的答案( 在UILabel的NSAttributedString中创build可点击的“链接” ),并自己编写一个演示。 但是,对我来说,演示是不好的。 这里是我的代码:(每个人都可以直接复制下面的代码到一个新的项目来进行testing) #import "ViewController.h" @interface ViewController () { UILabel* label; NSTextContainer *textContainer; NSLayoutManager *layoutManager; } @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; [super viewDidLoad]; label=[[UILabel alloc]initWithFrame:CGRectMake(15, 30, 350, 300)]; label.backgroundColor=[UIColor greenColor]; label.userInteractionEnabled = YES; UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnLabel:)]; [tap setNumberOfTapsRequired:1]; [label addGestureRecognizer:tap]; [self.view addSubview:label]; NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:@"String with a […]

在我编程实例化的UITextView(用NSTextContainer初始化).text属性总是零

[更新解决scheme和工作代码在底部] 在-viewDidLoad我分配,initWithFrame: 将myTextView添加到子视图 设置一些基本属性(alignment,背景颜色,文本颜色等) 设置默认文本 .text不会出现。 出现myTextView(如背景颜色所示),设置断点,它有一个框架,内存等。一切看起来都正确。 myTextView看起来不错,但是.text是零。 我改变它,设置它,更新它。 不pipe什么文字仍然是零。 我一遍又一遍地阅读文档。 没有提到我没有做的事情。 我不知所措 帮帮我。 在@interface MyController()… 以前所有的东西都是(弱),但为了以防万一,我把它变成了(强)。 没有骰子。 @property (strong, nonatomic) UIScrollView *scrollView; @property (strong, nonatomic) UIImageView *imageView; @property (strong, nonatomic) UILabel *titleLabel; @property (strong, nonatomic) UITextView *contentTextView; 在viewDidLoad中… – (void)viewDidLoad { [super viewDidLoad]; // scroll view CGSize size = CGSizeMake(703, self.view.frame.size.width); UIScrollView *aScrollView = […]

NSLayoutManager:调用setLocation(_:forStartOfGlyphRange :)将禁用整个string中的字距?

我创build了简单的NSLayoutManager子类,允许为定义的子string设置自定义的布局位置: class PositionableLayoutManager: NSLayoutManager { var xOffsetsPerGlyphRange = [(NSRange, CGPoint)]() override func invalidateLayoutForCharacterRange(charRange: NSRange, actualCharacterRange actualCharRange: NSRangePointer) { super.invalidateLayoutForCharacterRange(charRange, actualCharacterRange: actualCharRange) for (range, offset) in xOffsetsPerGlyphRange { setLocation(offset, forStartOfGlyphRange: range) } } } 现在,我可以像这样定义任何字符范围的自定义位置: let glyphRange = layoutManager.glyphRangeForCharacterRange(charRange, actualCharacterRange: nil) layoutManager.xOffsetsPerGlyphRange.append((glyphRange, customPointLocation)) layoutManager.invalidateLayoutForCharacterRange(NSRange(location: 0, length: textStorage.length), actualCharacterRange: nil) 它工作正常,唯一的问题是,这种方式重新定位的任何字形范围都布置和绘制没有字间距。 这里是一个例子: 在绿框中使用我的PositionableLayoutManager绘制的文本没有字距,在粉红色的框中有与使用UILabel绘制的kering相同的文本进行比较。 我怎么能做同样的事情,但适当的字距?

Swift:点击UILabel的一部分文本

我有一个问题,“boundingRectForGlyphRange”总是返回CGRect.zero“0.0,0.0,0.0,0.0”。 “boundingRectForGlyphRange”不起作用。 例如,我正在编写触摸UILabelfunction的一部分文本。 我的文本有第一部分是“任何文本”,第二部分是“读更多”。 当我点击“阅读更多”时,我只需要点击识别器就可以工作。 如果我触及UILabel的任何一点,“CGRectContainsPoint”总是返回true,然后调用action 在这里我的代码: override func viewDidLoad() { super.viewDidLoad() // The full string let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)]) firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSRange(location: 0, length: firstPart.length)) info.appendAttributedString(firstPart) // The "Read More" string that should be touchable let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", attributes: [NSFontAttributeName: […]

使用CALayer来突出显示跨越多行的UITextView中的文本

这是为了使用CALayer突出显示而在UITextView中获取CGRect文本的延续。 我无法获得每个线段中的范围正确的矩形。 NSString* searchString = @"Returns the range of characters that generated"; NSRange match = [[[self textView]text]rangeOfString:searchString]; NSRange matchingGlyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL]; [manager enumerateLineFragmentsForGlyphRange:matchingGlyphRange usingBlock: ^(CGRect lineRect, CGRect usedRect, NSTextContainer *textContainer, NSRange lineRange, BOOL *stop) { NSRange currentRange = NSIntersectionRange(lineRange, matchingGlyphRange); [manager enumerateEnclosingRectsForGlyphRange:currentRange withinSelectedGlyphRange:NSMakeRange(NSNotFound, 0) inTextContainer:textContainer usingBlock: ^(CGRect rect, BOOL* stop) { if (usedRect.origin.y […]