ios - 动态改变UIImage宽度

标签 ios objective-c uiimageview uiimage

我有这样的 UIImage,其中 UITextField 的位置位于左侧红线之间的空闲空白处: example

UIImage 的实际边框比可见部分大,因为手势识别器链接到该图像,并且它需要更大才能更舒适地使用手势。

文本字段中的文本对齐设置为右侧。因此,任务是当输入文本后键盘关闭时,根据输入的文本长度从左侧裁剪图像框架。我使用了这段代码:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
            ruleImage.layer.anchorPoint = CGPointMake(1,1);
            [ruleImage setFrame:CGRectMake(0, 0, 120 + ruleTextfield.text.length * 15 , ruleImage.frame.size.height)];
}

但是这段代码从左到右水平压缩图像,而不是裁剪。所以问题:

  • 如何将 anchor 设置为右上角(或下角)?
  • 我可以使用什么属性来裁剪图像?

附言我也尝试过

ruleImage.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
ruleImage.contentMode = UIViewContentModeTopRight;

但是这个属性没有解决我的问题。

最佳答案

尝试

ruleImage.contentMode = UIViewContentModeScaleAspectFill;

关于ios - 动态改变UIImage宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586507/

相关文章:

ios - UIScrollView 消息传递应用程序行为

iOS Settings Bundle 包含仅出现在调试版本中的项目

ios - 应用程序 :didReceiveLocalNotification: not called when app in background

ios - 滑动以在新的 View Controller 中显示第二张图片

ios - 如何从 GCD 返回 UIImage 类型

ios - 单击 CollectionViewCell 中的 ImageView 以转到 ViewController

ios - 动态添加 UIButtons 到屏幕

ios - 如何使用PHAsset获取[PHAssetCollection]

ios - Objective C 框架动画

iphone - 无法在 didFinishLaunchingWithOptions 中制作动画