Hey there, I'm having difficulties with adopting the UIKeyInput Protocol with Core Text. I currently have most of it done as posted in the source code, however for some reason it glitches as it types, with incorrect font height for the first letter of each new line, which doesn't really make much sense since it's just drawing whatever is in the attributed NSMutableString.
- (void)localInit:(CGFloat)width
{
fontSize = 50.0;
scrollViewWidth = width;
font = @"Helvetica";
[self setNeedsDisplay];
}
- (void)calculateHeight {
/* this methode will get the text height and change the frame size of self (the view) */
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange = CFRangeMake(0,0);
CGSize textHeight = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, CFStringGetLength((CFStringRef)attrString)), NULL, CGSizeMake(scrollViewWidth, CGFLOAT_MAX), &fitRange);
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, scrollViewWidth, textHeight.height + 50);
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)hasText {
if (words.length > 0) {
return YES;
}
return NO;
}
#pragma mark -
#pragma mark Text editing
- (void)insertText:(NSString *)theText {
[self.words appendString:theText];
[self setNeedsDisplay];
}
- (void)deleteBackward {
NSRange theRange = NSMakeRange(self.words.length-1, 1);
[self.words deleteCharactersInRange:theRange];
[self setNeedsDisplay];
}
// If more code is needed, feel free to ask for more, all this basically does is manipulates the NSMutableArray using the UIView handling the text input (inside the scrollview) by setting the UIView as the keyboard's first responder.
Is there any way I can improve this? It doesn't make any sense, as pre-defined NSMutableStrings work fine, however those that are dynamically changed screw up on new lines, either being elongated, or only partially showing... Can someone please shed some light on my dilemma? Or provide any source code that may help me here on one of the least documented apis? :)