我正在使用iOS 7中的Text Kit,我在NSTextContainer禁区附近发现了很多奇怪的东西.
我有两种观点:a UITextView和简单的可拖动UIView; 作为UIView移动,我从UIView框架创建了一条bezier路径(调整到UITextView坐标空间内)并更新了UITextViews NSTextContainer的exclusionPaths数组 - 非常简单.
在第一个屏幕截图中,您可以看到Text Kit很好地将文本包装在矩形排除区域周围:

但是,当用户在其中引入换行符时UITextView,TextKit似乎认为禁区的垂直区域要大得多 - 看起来与换行符创建的空格一样高.bezier路径完全相同,所以这似乎是一个Text Kit问题(除非我做错了).

码:
ViewController.h:
@interface ViewController : UIViewController<UITextViewDelegate>
@property (nonatomic, strong) IBOutlet UITextView *textView;
@property (nonatomic, strong) IBOutlet UIView *dragView;
@end
Run Code Online (Sandbox Code Playgroud)
ViewController.m:
-(void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self.dragView addGestureRecognizer:panRecognizer];
[self updateExclusionZone];
}
-(void)move:(UIPanGestureRecognizer *)pan
{
[self.view bringSubviewToFront:[pan view]];
if …Run Code Online (Sandbox Code Playgroud) 我有一个连接到平移手势识别器的 UITextView。当我将手指拖到文本视图上时,我使用 characterIndexForPoint 方法来确定我的手指在哪个字符上,然后在该点突出显示文本。
在视图中的文本有换行符的某些情况下,即使我传入相同的参数,此方法似乎返回不同的结果。touch 方法被连续调用两次,一次 charIndex 是 167,然后是 270。
我检查了文本视图的 AttributedString 并且在两种情况下它的字体大小相同。
- (IBAction)touched:(UIPanGestureRecognizer *)sender {
if (self.txtView.isFirstResponder) {
return;
}
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint touchPoint = [sender locationInView:self.txtView];
NSUInteger charIndex = [self.txtView.layoutManager
characterIndexForPoint:touchPoint
inTextContainer:self.txtView.textContainer
fractionOfDistanceBetweenInsertionPoints:0];
...
Run Code Online (Sandbox Code Playgroud)
这是一个已知的错误?难道我做错了什么?
尝试在TabelViewCell上使用不同的字体和字体大小,但它不起作用.
这是我尝试过的:
cell.textLabel.attributedText = [infoCells objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
此代码使应用程序崩溃
然后我尝试了这个:
cell.textLabel.text = [infoCells objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
现在一切正常但字体和字体大小不会改变.
以下是我的应用中的一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
infoCells = [[NSMutableArray alloc] initWithCapacity:2];
[infoCells addObject:@"DOI"];
[infoCells addObject:@"WIS?"];
[infoCells addObject:@"?????????? ?????? ????????????? ?????????? ???????????????? ????????????"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.attributedText = [infoCells objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Adobe Arabic" size:20];
return cell;
}
Run Code Online (Sandbox Code Playgroud) 我试图在UITextView中绘制透明的CALayer,以突出显示搜索中匹配的文本.
我已经找到了正确的方法来做到这一点,但仍然没有找到正确的坐标.我需要找到文本容器的来源.现在,我得到textView的原点,并通过它来抵消图层的原点:
NSRange match = [[[self textView]text]rangeOfString:@"predict the future"];
NSRange glyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];
CGRect textRect = [manager boundingRectForGlyphRange:glyphRange inTextContainer:[[self textView]textContainer]];
CGPoint textViewOrigin = self.textView.frame.origin;
textRect.origin.x += (textViewOrigin.x / 2);
textRect.origin.y += (textViewOrigin.y / 2);
CALayer* roundRect = [CALayer layer];
[roundRect setFrame:textRect];
[roundRect setBounds:textRect];
[roundRect setCornerRadius:5.0f];
[roundRect setBackgroundColor:[[UIColor blueColor]CGColor]];
[roundRect setOpacity:0.2f];
[roundRect setBorderColor:[[UIColor blackColor]CGColor]];
[roundRect setBorderWidth:3.0f];
[roundRect setShadowColor:[[UIColor blackColor]CGColor]];
[roundRect setShadowOffset:CGSizeMake(20.0f, 20.0f)];
[roundRect setShadowOpacity:1.0f];
[roundRect setShadowRadius:10.0f];
[[[self textView]layer]addSublayer:roundRect];
Run Code Online (Sandbox Code Playgroud)
如果我移动文本字段,或者如果我不将偏移量除以2,则得到以下结果:
我想知道我是否在正确的轨道上,以及如何找到NSTextContainer对象的起源.
我目前正在使用Swift进行开发,并且遇到了动态类型的问题.我已经设置了这段代码
import Foundation
import UIKit
class ExerciseController :UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var exerciseTableView :UITableView
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reload", name: UIContentSizeCategoryDidChangeNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIContentSizeCategoryDidChangeNotification, object: nil)
}
func reload() {
println("reload")
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { …Run Code Online (Sandbox Code Playgroud) 我有一个自定义项UITextView,它通过定义一个自定义NSTextStorage类来利用Apple的TextKit ,但是,当我将我的子类用于自定义文本视图的文本存储时(如下所述),并尝试打开任何大于20.0KB的文件,该应用将因崩溃而崩溃到内存泄漏:“ Message from debugger: Terminated due to memory issue”。
奇怪的是,如果我BMTextStorage只用一个标准的替换我的自定义NSTextStorage,则文本立即加载而没有任何内存泄漏,并且使用了小于30MB的RAM。是什么原因造成的?
TextView.swift
class TextView : UITextView {
required init(frame: CGRect) {
// If I replace the following line with simply
// "let textStorage = NSTextStorage()"
// I can open any file of any size and not have a memory leak
// issue, using only about 20-30MB of RAM. If I ran this code
// as is, it can open most files …Run Code Online (Sandbox Code Playgroud) 我有一个标准的基本Mac应用程序NSTextView.我正在尝试实现并使用子类NSTextStorage,但即使是非常基本的实现也会破坏列表编辑行为:
这是一个快速视频:
两个问题:
当我不替换文本存储时,这工作正常.
这是我的代码:
ViewController.swift
@IBOutlet var textView:NSTextView!
override func viewDidLoad() {
[...]
textView.layoutManager?.replaceTextStorage(TestTextStorage())
}
Run Code Online (Sandbox Code Playgroud)
TestTextStorage.swift
class TestTextStorage: NSTextStorage {
let backingStore = NSMutableAttributedString()
override var string: String {
return backingStore.string
}
override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [NSAttributedString.Key:Any] {
return backingStore.attributes(at: location, effectiveRange: range)
}
override func replaceCharacters(in range: NSRange, with str: String) {
beginEditing()
backingStore.replaceCharacters(in: range, with:str)
edited(.editedCharacters, range: range,
changeInLength: (str as NSString).length - …Run Code Online (Sandbox Code Playgroud) 在WWDC 2013视频中,他们表明开发人员可以对文本使用凸版印刷效果.有没有人有任何示例代码如何使用UILabel执行此操作/如何执行此操作?
我正在使用textStorage的属性UITextView.我有字符串和我班级的对象数组TextFormattingElement.此类的一个实例由NSRange(必须在文本中应用此元素)和一些格式化参数组成:
@interface TextFormattingElement : NSObject
@property (nonatomic) NSRange range;
@property (nonatomic, strong) NSString *fontName; //e.g. @"TimesNewRomanPSMT"
@property (nonatomic) int fontSize;
@property (nonatomic, strong) UIColor *fontColor;
@property (nonatomic) BOOL isBold;
@property (nonatomic) BOOL isItalic;
@property (nonatomic) BOOL isUnderlined;
@property (nonatomic) BOOL isStriked;
@end
Run Code Online (Sandbox Code Playgroud)
现在我遍历这个数组,并连续将这些元素应用到textView的textStorage.我用这个方法:
-(void)setFontWithName:(NSString*)name AndSize:(float)fontSize AndTextColor:(UIColor*)textColor AndIsBold:(BOOL)isBold AndIsItalic:(BOOL)isItalic AndIsUnderlined:(BOOL)isUnderLined andIsStriked:(BOOL)isStriked ToRange:(NSRange)rangeToSet{
__block UIFont *font = [UIFont fontWithName:name size:fontSize];
__block UIFontDescriptor *fontDescriptor = [font fontDescriptor];
[textView.textStorage enumerateAttributesInRange:rangeToSet options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { …Run Code Online (Sandbox Code Playgroud) 我正在创建一系列 NSTextContainer 来保存 HTML 资源中的文本。我可以将 HTML 添加到属性字符串,将其分配给 NSTextStorage 和 NSLayoutManager,并创建一系列 NSTextContainer 来保存所有文本。
我的问题是,我想在文本中添加“分页符”,即停止填充此文本容器并开始另一个...在文档中,我发现了一些名为 NSControlCharacterContainerBreakAction; 的内容。但我不清楚如何实施它,或者这是否是最好的方法。
下面的代码片段是我当前如何通过文本容器(在 Swift 中)构建的。
var myLayoutManager = NSLayoutManager()
var myText:NSAttributedString = {
let path = NSBundle.mainBundle().URLForResource("localfile", withExtension: "html")
let opts = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
return NSMutableAttributedString(fileURL: path, options: nil, documentAttributes: nil, error: nil)!
}()
myTextStorage = NSTextStorage(attributedString: myText)
myTextStorage.addLayoutManager(myLayoutManager)
//Create all textContainers to hold text
if myLayoutManager.textContainers.count == 0 {
var range = NSMakeRange(0, 0)
while(NSMaxRange(range) < myLayoutManager.numberOfGlyphs) {
var myTextContainer = NSTextContainer(size: CGSizeMake(450, 580))
myLayoutManager.addTextContainer(myTextContainer)
range = …Run Code Online (Sandbox Code Playgroud) textkit ×10
ios ×6
ios7 ×3
objective-c ×3
swift ×3
uitextview ×3
calayer ×1
cocoa ×1
cocoa-touch ×1
ipad ×1
iphone ×1
letterpress ×1
nstextview ×1
uilabel ×1