文本自适应

Fit to size of UI element with child Text

If you have a UI element, such as a Button, that has a background image and a child Game Object with a Text component on it, you probably want the whole UI element to fit the size of the text - maybe with some padding.

In order to do this, first add a Horizontal Layout Group to the UI element, then add a Content Size Fitter too. Set the Horizontal Fit, the Vertical Fit, ot both to the Preferred setting. You can add and tweak padding using the padding property in the Horizontal Layout Group.

Why use a Horizontal Layout Group? Well, it could have been a Vertical Layout Group as well - as long as there is only a single child, they produce the same result.

How does it work?

The Horizontal (or Vertical) Layout Group functions both as a Layout Controller and as a Layout Element. First it listens to the layout information provided by the children in the group - in this case the child Text. Then it determines how large the group must be (at minimum, and preferrably) in order to be able to contain all the children, and it functions as a Layout Element that provides this information about its minimum and preferred size.

The Content Size Fitter listens to layout information provided by any Layout Element on the same Game Object - in this case provided by the Horizontal (or Vertical) Layout Group. Depending on its settings, it then controls the size of the Rect Transform based on this information.

Once the size of the Rect Transform has been set, the Horizontal (or Vertical) Layout Group makes sure to position and size its children according to the available space. See the page about the Horizontal Layout Group for more information about how it controls the positions and sizes of its children.

时间: 2024-10-12 17:27:34

文本自适应的相关文章

UILabel自适应文本,让文本自适应

//UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)]; label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; label.text = @"本店于十一期间特推出一系列优惠,限时限量敬请选购!沙发:钻石品质,首领风范!床垫:华贵典雅,彰显时尚!尊贵而不失奢华,典雅却不失自

Storyboard实现简单的文本自适应

建好工程,来到Storyboard,开搞. 首先设置竖屏 在Storyboard拖入一个滚动视图UIScrollView 由于这边只是演示,所以直接给ScrollView加 上,左,下,右的约束为0: 现在更新一下得到如下效果 接下来就是重点了! 我们在ScrollView上拖入一个UIView 现在我们来为View 加约束 我们先设定上,左,右的约束,全部为0 但是我们发现得到如下 说明我们并没有加足够的约束用来约束他 少了什么呢? 我们这边要做的是上下滚动的,少了个高度?那现在我们给他一个高

ios之UILabel实现文本自适应方法

UILabel实现文本自适应方法(ios7) - (void)initUserInterface { UILabel *label = [[UILabel alloc]init]; label.numberOfLines = 0; // 须要把显示行数设置成无限制 label.font = [UIFont systemFontOfSize:15]; label.textAlignment = NSTextAlignmentCenter; label.text = @"xxxxxxxxxxxxxx

怎样让自定义Cell的图片和文本自适应高度

Let's do it! 首先创建一个Model类 包括一个图片名称属性 还有文字内容属性 #import <Foundation/Foundation.h> @interface Model : NSObject @property (nonatomic, copy) NSString *imageName; @property (nonatomic, copy) NSString *info; @end 创建一个继承于UITableViewCell的MyTableViewCell,把模型作

Html大段文本自适应换行显示-SSM

只处理前端: <style> .ctl{ table-layout:fixed } .ctl td{ word-break:break-all } </style> <div> <table cellSpacing="0" cellpadding="1" width="100%" class="ctl" border=1> <tBody> <tr><

k3 cloud中单据体中文本自适应

在单据体中添加多行文本,然后设置本地配置,只读单元格自动换行 原文地址:https://www.cnblogs.com/woshinige/p/11684725.html

iOS常用技术-气泡文本自适应

////  ChatBubble.h//  ChatBubble////  Created by 大欢 on 16/1/21.//  Copyright © 2016年 bjsxt. All rights reserved.// #import <UIKit/UIKit.h> @interface ChatBubble : UIImageView //显示的文字@property (nonatomic, copy) NSString * text; @end/*****************

ios根据文本自适应 然后 搭建类似如下效果

UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tbSecond.size.width, 0)]; headView.backgroundColor = [UIColor whiteColor]; UIView * view = [[UIView alloc]initWithFrame:CGRectMake(30, 40, self.tbSecond.bounds.size.width - 90, 0)

UIlabel 文本高度自适应 不同系统出现问题

在文本自适应中,7 .8 系统不一样,对于文本计算方式也不同 + (CGSize)getTextHeight:(NSString *)text withFont:(UIFont *)font withCGSize:(CGSize)Size{ NSString *inputString = [NSString stringWithFormat:@"%@",text]; CGSize titleSize; if ([[[UIDevice currentDevice] systemVersi