iOS8 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 的替代方法

本文深入探讨了Objective-C在iOS开发中的应用,包括其在iOS应用开发中的优势、核心概念、常见用法及最佳实践。

NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
CGSize size = [label.text boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;

有没有暗黑模式相关的使用?下面的h文件声明 // // TPBBaseViewController.h // TPBDesignKit // // Created by ming on 2024/1/20. // Copyright © 2024 TP-Link. All rights reserved. // #import "TPBBaseNavigationController.h" NS_ASSUME_NONNULL_BEGIN @class MASViewAttribute; //屏幕旋转的方向,UIDeviceOrientation方向转换为6个方向 typedef NS_ENUM(NSInteger, TPBInterfaceOrientation) { TPBInterfaceOrientationUnknow = 0, TPBInterfaceOrientationUp = 1, TPBInterfaceOrientationDown = 2, TPBInterfaceOrientationLeft = 3, TPBInterfaceOrientationRight = 4, }; @interface TPBBaseViewController : UIViewController @property (nonatomic, readonly, strong) UINavigationController *tpbNavigationController; /// 转换后的方向,上下左右 @property (nonatomic, assign) TPBInterfaceOrientation orientation; /// 设置导航栏是否全透明 @property (nonatomic, assign) BOOL isNavigationBarTransparent; /// 设置导航栏是否隐藏 @property (nonatomic, assign) BOOL isNavigationBarHidden; /// 导航栏大标题字体 @property (nonatomic, strong) UIFont *navigationLargeTitleFont; /// 导航栏正常标题字体 @property (nonatomic, strong) UIFont *navigationTitleFont; @property (nonatomic, readonly, strong) MASViewAttribute *mas_tpSafeAreaLayoutGuide; - (void)tpbSetupInitialData NS_REQUIRES_SUPER; - (void)tpbSetupSubviews NS_REQUIRES_SUPER; - (void)tpbMakeConstraint NS_REQUIRES_SUPER; - (void)tpbBindActions NS_REQUIRES_SUPER; /// 用户直接点击导航栏返回按钮 - (void)handleBackClick; /// 当检测到测划返回手势时,是否要响应返回手势(可在此方法做数据检测以及弹框提示) - (BOOL)shouldRespondToPopGesture; /// 监听屏幕旋转事件,子类复写 - (void)orientationChangeAction; /// 隐藏键盘 - (void)tpbHideKeyboard; - (void)updateStatusBarHidden:(BOOL)hideStatusBar; /// 记住当前页面VoiceOver焦点 - (void)tpbSaveFocusElement; /// 恢复当前页面VoiceOver焦点 - (void)tpbRestoreFocusElement; @end #pragma mark - TPBBaseViewController (TPBNavigationBar) @interface TPBBaseViewController (TPBNavigationBar) /// 设置是否采用Large Title - (void)updatePrefersLargeTitle:(BOOL)prefersLargeTitle; /// 设置侧滑返回是否生效(默认开启) - (void)setInteractivePopGestureRecognizerEnabled:(BOOL)isEnable; /// 设置导航栏标题(仅在当前页面生效) - (void)setNavigationBarTitle:(NSString * _Nullable)title; - (void)setNavigationBarAppearanceTransparent; - (void)setNavigationBarWithBarColor:(UIColor *)barColor textColor:(UIColor *)textColor textFont:(UIFont *)textFont isTransparent:(BOOL)isTransparent; @end #pragma mark - UIViewController (TPBBarButton) @interface UIViewController (TPBBarButton) // 自定义导航栏按钮 // 方式一:文字按钮 - (UIBarButtonItem *)tpbCreateLeftBarButtonItemWithText:(nullable NSString *)text andTextColor:(nullable UIColor *)textColor andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateLeftBarButtonItemWithText:(nullable NSString *)text andNormalTextColor:(nullable UIColor *)normalColor andHighlightTextColor:(nullable UIColor *)highlightColor andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateRightBarButtonItemWithText:(nullable NSString *)text andTextColor:(nullable UIColor *)textColor andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateRightBarButtonItemWithText:(nullable NSString *)text andNormalTextColor:(nullable UIColor *)normalColor andHighlightTextColor:(nullable UIColor *)highlightColor andTarget:(nullable id)target andAction:(nullable SEL)action; // 方式二:图片按钮 - (UIBarButtonItem *)tpbCreateLeftBarButtonItemWithImage:(nullable UIImage *)image andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateLeftBarButtonItemWithImage:(nullable UIImage *)image imageTintColor:(nullable UIColor *)imageTintColor andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateRightBarButtonItemWithImage:(nullable UIImage *)image andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateRightBarButtonItemWithImage:(nullable UIImage *)image imageTintColor:(nullable UIColor *)imageTintColor andTarget:(nullable id)target andAction:(nullable SEL)action; // 方式四:自定义视图按钮 - (UIBarButtonItem *)tpbCreateBarButtonItemWithCustomView:(UIView *)customView size:(CGSize)size andTarget:(nullable id)target andAction:(nullable SEL)action; - (UIBarButtonItem *)tpbCreateFixedSpaceBarButtonItemWithWidth:(CGFloat)width; @end NS_ASSUME_NONNULL_END
最新发布
12-03
从你提供的 `TPBBaseViewController.h` 头文件来看,**没有直接声明与暗黑模式相关的属性或方法**,比如: - 没有 `traitCollection` 相关的监听或重写; - 没有 `overrideUserInterfaceStyle`(iOS 13+)的设置; - 没有与 `UIUserInterfaceStyleDark` 相关的判断或配置; - 也没有声明使用 `UIColor` 的 `colorWithDynamicProvider:` 方法--- ## ✅ 分析结论: ### ❌ **当前头文件中未直接支持暗黑模式** 但我们可以从几个角度进一步分析: --- ### 🔍 1. **是否间接支持暗黑模式?** 虽然头文件中没有直接声明暗黑模式相关方法,但可能存在以下情况: - `TPBBaseViewController` 的子类可能在 `.m` 文件中重写了 `traitCollectionDidChange:` 方法- `setNavigationBarWithBarColor:textColor:textFont:isTransparent:` 方法中传入的 `barColor` 和 `textColor` 可能是动态颜色(如通过 Asset Catalog 或 `colorWithDynamicProvider:` 创建); - `TPBBaseNavigationController` 可能统一处理了暗黑模式下的导航栏样式; - 项目中可能有全局的暗黑模式管理类(如 Appearance Manager),统一设置 `overrideUserInterfaceStyle`。 --- ### 🔍 2. **可能支持暗黑模式的方式(推测)** #### ✅ 方法一:使用动态颜色设置导航栏 ```objc [self setNavigationBarWithBarColor:[UIColor dynamicColorForDarkMode] textColor:[UIColor dynamicTextColorForDarkMode] textFont:[UIFont systemFontOfSize:17] isTransparent:NO]; ``` #### ✅ 方法二:子类实现 traitCollection 监听 ```objc - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; if (self.traitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle) { [self updateUIForCurrentAppearance]; } } ``` #### ✅ 方法三:全局设置暗黑模式样式(如在 AppDelegate 或 Appearance Manager 中) ```objc if (@available(iOS 13.0, *)) { [[UITraitCollection standardUserDefaults] setOverrideTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] forViewController:self.window.rootViewController]; } ``` --- ### ✅ 总结:暗黑模式支持情况分析 | 类别 | 内容 | |------|------| | **当前头文件是否有暗黑模式支持?** | ❌ 否 | | **是否可能在子类或实现文件中支持?** | ✅ 是 | | **是否可能通过全局样式统一管理?** | ✅ 是 | | **建议** | 可添加 `traitCollectionDidChange:` 方法监听,使用动态颜色设置 UI 元素 | --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值