【iOS - 周總結】開發中遇到的小知識點(2018.12.10-2018.12.15)
阿新 • • 發佈:2018-12-15
1.WKWebview載入html文字圖片過大,沒有自適應螢幕寬高。
在用Webview載入html文字有時候會遇到載入的圖片過大,不能自適應螢幕寬高的問題。那麼如何解決這個問題?如何使圖片自適應螢幕?很簡單,只需要加一個js就可以。
直接上程式碼:
- (WKWebView *)detailWeb { if (!_detailWeb) { NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}"; WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController *wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; WKWebViewConfiguration*wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; _detailWeb = [[WKWebView alloc] initWithFrame:CGRectMake(0, AdaptedWidth(40), SCREEN_WIDTH, SCREEN_HEIGHT-TopNavHeight-AdaptedWidth(40)) configuration:wkWebConfig]; _detailWeb.backgroundColor= [UIColor whiteColor]; _detailWeb.navigationDelegate = self; } return _detailWeb; }
2.scrollview使用Masnory自動佈局
近期在專案中使用了Masnory。在一個關於我們的頁面由於資料網路請求下來,且內容長度不一定,我就決定使用Masnory+ScrollView來實現。結果就出現了一些些問題。
使用過程就在程式碼裡顯示。
#import "ViewController.h" #import "Masonry/Masonry.h" @interface ViewController () @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIView *scrollContentView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *subLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"1"; // 1.把scrollView新增到控制器view [self.view addSubview:self.scrollView]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; // 2.給scrollView新增一個containerView // 重點 唯一 大小貼合scrollview // 即scrollView的子檢視,即過渡檢視contentSize,並設定其約束 [self.scrollView addSubview:self.scrollContentView]; [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) { // 注意點:首先確定contentView的位置。如果上下滑動等寬 左右滑動等高 類似於xib make.left.top.right.bottom.equalTo(self.scrollView); make.width.equalTo(self.scrollView); }]; // 3.此後所有檢視新增在scrollContentView上 [self.scrollContentView addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { // 注意點這裡面設定子檢視都要新增在contentView上 必須注意 make.left.equalTo(self.scrollContentView.mas_left).offset(20); make.right.equalTo(self.scrollContentView.mas_right).offset(-20); make.top.equalTo(self.scrollContentView.mas_top).offset(100); make.height.mas_equalTo(40); }]; [self.scrollContentView addSubview:self.subLabel]; [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.scrollContentView.mas_left).offset(20); make.right.equalTo(self.scrollContentView.mas_right).offset(-20); make.top.equalTo(self.titleLabel.mas_bottom).offset(40); // 這個可寫在這個位置 也可以在網路請求結束後新增 make.bottom.equalTo(self.scrollContentView.mas_bottom).offset(-100); }]; // 模擬網路請求 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.subLabel.text = @"精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦精雕機哦跌跤就疊加偶家迪歐家餓哦就掉得哦啊基金的年紀大遞交炯炯都我阿爾加到的驕傲的 待機餓哦案件都安靜哦啊接打帶哦接啊哦啊接待偶爾接愛哦的舊愛帶哦精雕機帶偶覺得 基調而驕傲的奧迪偶記帶哦"; // 在上面寫了這個位置可以不寫 最好不要都寫 會造成程式碼混亂 // // 設定過渡檢視的底邊距(此設定將影響到scrollView的contentSize) // [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) { // make.bottom.equalTo(self.subLabel.mas_bottom).offset(30); // }]; }); // Do any additional setup after loading the view, typically from a nib. } #pragma mark - Lazy Setter - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.backgroundColor = [UIColor greenColor]; } return _scrollView; } - (UIView *)scrollContentView { if (!_scrollContentView) { _scrollContentView = [[UIView alloc] init]; _scrollContentView.backgroundColor = [UIColor redColor]; } return _scrollContentView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.text = @"title"; _titleLabel.textColor = [UIColor blackColor]; _titleLabel.backgroundColor = [UIColor whiteColor]; _titleLabel.font = [UIFont systemFontOfSize:16]; _titleLabel.numberOfLines = 1; } return _titleLabel; } - (UILabel *)subLabel { if (!_subLabel) { _subLabel = [[UILabel alloc] init]; _subLabel.textColor = [UIColor blackColor]; _subLabel.backgroundColor = [UIColor whiteColor]; _subLabel.font = [UIFont systemFontOfSize:16]; _subLabel.numberOfLines = 0; } return _subLabel; } @end
3.設定label的最小字型。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 30)]; label.text = @"fjirfjirjfi積分敵人的積極對得金蝶雞的解決跌阿達啊打發 啊打發"; label.adjustsFontSizeToFitWidth = YES; label.font = [UIFont systemFontOfSize:18]; label.textColor = [UIColor blackColor]; // label.minimumFontSize = 14; label.minimumScaleFactor = 0.5; [self.view addSubview:label];
在6.0系統後minimumFontSize屬性已經被棄用。推薦使用minimumScaleFactor。
注意點:
1、0<minimumScaleFactor<1時才能達到效果。(字型10,想要最小字型5,設定0.5即可)
2、另外要設定adjustsFontSizeToFitWidth=YES.
3、還有需要UIlabel的行數是1的時候才有用,多行的label是不行的
4、minimumScaleFactor預設值是0,此時最小字型就是設定的字型大小,和1的效果一樣。