我已在我的应用中编写此代码:
- (IBAction)ZoomInFunction{
@try{
UITextField *test = (UITextField *)[self.view viewWithTag:indexNews];
NSLog(@"INDEX NEWS : %d", indexNews);
UIFont *font = test.font;
if(test.font == [font fontWithSize:22])
test.font = [font fontWithSize:22];
else
test.font = [font fontWithSize:font.pointSize+2];
}@catch (NSException *err) {
NSLog(@"Error handler : %@", err);
}
}
- (IBAction)ZoomOutFunction{
@try {
UITextField *test = (UITextField *)[self.view viewWithTag:indexNews];
UIFont *font = test.font;
if(test.font == [font fontWithSize:14])
test.font = [font fontWithSize:14];
else
test.font = [font fontWithSize:font.pointSize-2];
}@catch (NSException *err) {
NSLog(@"Error handler : %@", err);
}
有时代码运行良好,但有时会显示错误,如此。
错误处理程序: - [UIView font]:无法识别的选择器发送到实例0xac70780
答案 0 :(得分:1)
仔细阅读此Apple文章,您将了解IOS中的放大和缩小功能。
以下是行动中的代码:
UITextView *textView = [UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeFontSize:)];
[textView addGestureRecognizer:gestureRecognizer];
- (void)changeFontSize:(UIPinchGestureRecognizer *)gestureRecognizer
{
UITextView *textView = (UITextView *)gestureRecognizer.view;
float yourFontSize = gestureRecognizer.scale * FONT_SIZE;
textView.font = [UIFont systemFontOfSize:yourFontSize];
}
答案 1 :(得分:1)
可以在UITextView上进行缩放。 UITextView在键入文本时动态扩展,并在用户捏住屏幕时进行缩放(类似行为可在TinyPost中找到)。
在viewDidLoad
中应用此功能 UITextView *test = (UITextView *)[self.view viewWithTag:indexNews];
UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTextView:)];
[test addGestureRecognizer:gestureRecognizer];
并像这样在UITextView上应用zoomIn和out
- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{
CGFloat scale = pinchGestRecognizer.scale;
createTextView.font = [UIFont fontWithName:createTextView.font.fontName size:createTextView.font.pointSize*scale];
[self textViewDidChange:createTextView];
}
- (void)textViewDidChange:(UITextView *)textView{
CGSize textSize = textView.contentSize;
textView.frame = CGRectMake(CGRectGetMinX(textView.frame), CGRectGetMinY(textView.frame), textSize.width, textSize.height); //update the size of the textView
}
我希望它适合你。
答案 2 :(得分:0)
我已经申请了,我得到了适当的解决方案。这是由于以下代码行
UITextField *test = (UITextField *)[self.view viewWithTag:indexNews];
有些时候你通过viewWithTag:indexNews从self.view获取UITextView,那么这个indexNews标记值已经分配给其他UIView,所以这行代码获取UIView而不是UITextField。所以它无法通过UIView调用Font方法,因此它返回错误。我有一个更好的解决方案
在你的.h文件中为UITextView实现一个插座
@property (weak, nonatomic) IBOutlet UITextView *fontTextView;
在.m文件中
@synthesize fontTextView;
- (IBAction)ZoomInFunction{
@try{
UIFont *font = fontTextView.font;
if(fontTextView.font == [font fontWithSize:22])
fontTextView.font = [font fontWithSize:22];
else
fontTextView.font = [font fontWithSize:font.pointSize+2];
}@catch (NSException *err) {
NSLog(@"Error handler : %@", err);
}
}
- (IBAction)ZoomOutFunction{
@try {
UIFont *font = fontTextView.font;
if(fontTextView.font == [font fontWithSize:14])
fontTextView.font = [font fontWithSize:14];
else
fontTextView.font = [font fontWithSize:font.pointSize-2];
}@catch (NSException *err) {
NSLog(@"Error handler : %@", err);
}
}
我希望它适用于您的代码。感谢