我需要在UITableView中缩放文本大小与缩放放大ios我正在尝试但无法做到这一点(i need to scale text size in UITableView with pinch to z

编程入门 行业动态 更新时间:2024-10-23 21:38:27
我需要在UITableView中缩放文本大小与缩放放大ios我正在尝试但无法做到这一点(i need to scale text size in UITableView with pinch to zoom in ios i am trying but cant able to do that) #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); CGFloat lastScaleFactor=1; CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (factor > 1) { //zooming in _textView.transform=CGAffineTransformMakeScale(lastScaleFactor + (factor-1),lastScaleFactor + (factor -1)); } else { _textView.transform=CGAffineTransformMakeScale(lastScaleFactor *factor, lastScaleFactor *factor +1); } if (sender.state==UIGestureRecognizerStateEnded) { if (factor >1) { lastScaleFactor += (factor-1); } else lastScaleFactor *= factor; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

我想完全像这样输入链接描述

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); CGFloat lastScaleFactor=1; CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (factor > 1) { //zooming in _textView.transform=CGAffineTransformMakeScale(lastScaleFactor + (factor-1),lastScaleFactor + (factor -1)); } else { _textView.transform=CGAffineTransformMakeScale(lastScaleFactor *factor, lastScaleFactor *factor +1); } if (sender.state==UIGestureRecognizerStateEnded) { if (factor >1) { lastScaleFactor += (factor-1); } else lastScaleFactor *= factor; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

i want to do exactly like that enter link description here

最满意答案

在链接的视频上,不是正在改变的视图变换,而是字体大小(注意文本是如何重排的)。 只需将计算出的比例应用于textView的字体大小就可以了。

其次,你的lastScaleFactor应该可以定义为static ,否则它没有意义......尝试下面的行(没有测试甚至编译,但应该直接复制)。 下面的代码段假定您在textView中有纯文本。 如果您已归因于文本,则必须更改将新字体分配给文本视图的位置,而是循环遍历所有attributedText段并相应地修改每个段大小; 你也必须保留每个文本段的最后固定字体大小的临时备忘录,如下面对currentFontSize属性中的整个文本所做的那样

@interface ViewController () @property CGFloat currentFontSize; @end

...

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.currentFontSize = _textView.font.pointSize } - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); const CGFloat scaleCoeff = 0.5; //play with this one CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (sender.state==UIGestureRecognizerStateEnded) { self.currentFontSize = _textView.font.pointSize; } else { _textView.font = [_textView.font fontWithSize:self.currentFontSize * factor * scaleCoeff]; } }

On the linked video it is not the view transform that is changing, it is the font size (note how the text is reflowed). Simply apply your calculated scale to the textView's font size and that's it.

Secondly, your lastScaleFactor should probably be defined static, otherwise it doesn't make sense... Try something along the below lines (was not tested or even compiled, however should be straight forward to copy). The snippet below assumes you have plain text in the textView. In case you have attributed text, then you would have to change the place where a new font is assigned to the text view, and instead loop through all attributedText segments and modify each of the segments size accordingly; also you would have to keep temporary memo of the last fixed font size per each of the text segments, as it is done below for the whole text in the currentFontSize property

@interface ViewController () @property CGFloat currentFontSize; @end

...

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.currentFontSize = _textView.font.pointSize } - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); const CGFloat scaleCoeff = 0.5; //play with this one CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (sender.state==UIGestureRecognizerStateEnded) { self.currentFontSize = _textView.font.pointSize; } else { _textView.font = [_textView.font fontWithSize:self.currentFontSize * factor * scaleCoeff]; } }

更多推荐

本文发布于:2023-07-24 01:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   这一点   文本   大小   ios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!