- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个带有图标的自定义消息单元格(在右侧用于传入,在左侧用于传出)
我已将 JSQMessagesCollectionViewCellOutgoing
子类化,并在我想要的位置显示图标。伟大的。我没能做的是让 TextView 的宽度更小以考虑到图标。我认为这是我的自定义传出 xib 文件中的一个简单的自动布局更改,但是当应用程序运行时,自动布局宽度被覆盖。
如何从 JSQMessagesCollectionViewCell
或 JSQMessagesCollectionViewCellOutgoing
的子类更新(直接或间接)JSQMessagesCollectionViewCell
中的 textViewMarginHorizontalSpaceConstraint
>
最佳答案
您不必创建 JSQMessagesCollectionViewCell 的任何扩展,
除非你想更改 Cell 的结构,比如更改发送者/接收者头像的位置或上/下标签位置。
我是 JSQMessage 的新手,但这是我的解决方案:
如果你只是想要消息的主要内容部分的自定义 View ,也就是气泡圆角部分:),那么
您只需要一个自定义的 JSQMediaItem 类,
然后你覆盖
- (UIView *)mediaView
方法,然后您就可以完全控制内容 View 。
技巧部分是,您必须先获取内容 View 的高度
- (CGSize)mediaViewDisplaySize
该方法由JSQ框架调用。所以最好的方法是在自定义类的 init 方法中动态计算返回 View 的高度:
- (instancetype)initWithImage:(UIImage *)image title:(NSString*)title message:(NSString*)msg
这是我的自定义类,需要显示事件消息、标题、图像和消息。顺便说一句,只有消息部分的高度是动态的,所以我只是使用
计算了这部分[NSString boundingRectWithSize:]
EventMediaItem.h
#import <JSQMessagesViewController/JSQMediaItem.h>
NS_ASSUME_NONNULL_BEGIN
/**
* The `EventMediaItem` class is a concrete `JSQMediaItem` subclass that implements the `JSQMessageMediaData` protocol
* and represents a event media message. An initialized `EvnetMediaItem` object can be passed
* to a `JSQMediaMessage` object during its initialization to construct a valid media message object.
* You may wish to subclass `EventMediaItem` to provide additional functionality or behavior.
*/
@interface EventMediaItem : JSQMediaItem <JSQMessageMediaData, NSCoding, NSCopying>
/**
* The image for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) UIImage *image;
/**
* The title for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) NSString *title;
/**
* The message for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) NSString *message;
/**
* Initializes and returns a event media item object having the given image.
*
* @param image The image for the event media item. This value may be `nil`.
* @param title The title for the event media item. This value may be `nil`.
* @param message The message for the event media item. This value may be `nil`.
*
* @return An initialized `EventMediaItem`.
*
* @discussion If the image must be dowloaded from the network,
* you may initialize a `EventMediaItem` object with a `nil` image.
* Once the image has been retrieved, you can then set the image property.
*/
- (instancetype)initWithImage:(UIImage *)image title:(NSString*)title message:(NSString*)msg;
@end
NS_ASSUME_NONNULL_END
和 EventMediaItem.m
#import "EventMediaItem.h"
#import "JSQMessagesMediaPlaceholderView.h"
#import "JSQMessagesMediaViewBubbleImageMasker.h"
#import "UIImage+JSQMessages.h"
#import "JSQMessagesBubbleImageFactory.h"
#import <MobileCoreServices/UTCoreTypes.h>
#define paddingStart 15.0f
#define indent 10.0f
#define spacing 7.0f
#define paddingTop 3.0f
#define heightTitle 40.0f
#define heightImage 100.0f
#define magicDelta 20.0f
@interface EventMediaItem (){
CGFloat heightMsgView;
NSDictionary *attrsDictionary;
}
@property (strong, nonatomic) UIView *rootView;
@property (strong, nonatomic) UIImageView *cachedImageView;
@property (strong, nonatomic) UILabel *titleView;
@property (strong, nonatomic) UITextView *messageView;
@end
@implementation EventMediaItem
#pragma mark - Initialization
- (instancetype)initWithImage:(UIImage *)image title:(NSString*)title message:(NSString*)msg
{
self = [super init];
if (self) {
_image = [image copy];
_cachedImageView = nil;
_message = [msg copy];
_messageView = nil;
_title = [title copy];
_titleView = nil;
CGSize size;
size = CGSizeMake(210.0, 500);
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
size = CGSizeMake(315.0, 500);
}
UITextView *tmp = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height-(paddingTop+heightImage+heightTitle))];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = indent;
paragraphStyle.firstLineHeadIndent = indent;
paragraphStyle.lineSpacing = spacing;
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:14.f];
// save the attrs for future use
attrsDictionary =
@{ NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName:font}; // <-- there are many more attrs, e.g NSFontAttributeName
tmp.attributedText = [[NSAttributedString alloc] initWithString:_message attributes:attrsDictionary];
// *** ADJUST VIEW SIZE ***
CGRect containSize = [tmp.text boundingRectWithSize:CGSizeMake(size.width, 400) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrsDictionary context:nil];
heightMsgView = containSize.size.height;
// *** ADJUST VIEW SIZE ***
}
return self;
}
- (void)clearCachedMediaViews
{
[super clearCachedMediaViews];
_cachedImageView = nil;
}
#pragma mark - Setters
- (void)setImage:(UIImage *)image
{
_image = [image copy];
_cachedImageView = nil;
}
- (void)setAppliesMediaViewMaskAsOutgoing:(BOOL)appliesMediaViewMaskAsOutgoing
{
[super setAppliesMediaViewMaskAsOutgoing:appliesMediaViewMaskAsOutgoing];
_cachedImageView = nil;
}
#pragma mark - JSQMessageMediaData protocol
- (UIView *)mediaView
{
UIColor *bgc = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
if (self.image == nil) {
return nil;
}
if (self.cachedImageView == nil) {
CGSize size = CGSizeMake(210.0, 400);
self.rootView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, paddingTop+heightTitle+heightImage+heightMsgView+magicDelta)];
self.rootView.backgroundColor = bgc;
self.titleView = [[UILabel alloc] initWithFrame:CGRectMake(paddingStart, paddingTop, size.width-15, heightTitle)];
self.titleView.font = [UIFont systemFontOfSize:16 weight:20];
self.titleView.textColor = [UIColor darkGrayColor];
self.titleView.numberOfLines = 2;
self.titleView.text = _title;
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.image];
imageView.frame = CGRectMake(0.0f, self.titleView.frame.size.height, size.width, heightImage);
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
self.cachedImageView = imageView;
self.messageView = [[UITextView alloc] initWithFrame:CGRectMake(00.0f, self.titleView.frame.size.height+self.cachedImageView.frame.size.height, size.width, heightMsgView+magicDelta)];
self.messageView.textColor = [UIColor darkGrayColor];
self.messageView.font = [UIFont systemFontOfSize:15];
self.messageView.backgroundColor = bgc;
self.messageView.editable = NO;
//---
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:_message attributes:attrsDictionary];
NSRange range = [self.message rangeOfString:NSLocalizedStringFromTable(@"EventListMore", @"Message", @"")];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:108/255.0 green:210/255.0 blue:240/255.0 alpha:1] range:range]; //6cd2f0
//---
self.messageView.attributedText = attrString;
//add the views to mediaView
[self.rootView addSubview:self.cachedImageView];
[self.rootView addSubview:self.messageView];
[self.rootView addSubview:self.titleView];
//apply bubble factory
JSQMessagesBubbleImageFactory *factory = [[JSQMessagesBubbleImageFactory alloc] initWithBubbleImage:[UIImage jsq_bubbleRegularTaillessImage] capInsets:UIEdgeInsetsZero];
JSQMessagesMediaViewBubbleImageMasker*masker = [[JSQMessagesMediaViewBubbleImageMasker alloc] initWithBubbleImageFactory:factory];
[masker applyIncomingBubbleImageMaskToMediaView:self.rootView];
}
return self.rootView;
}
- (CGSize)mediaViewDisplaySize
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
return CGSizeMake(315.0f, paddingTop+heightTitle+heightImage+heightMsgView);
}
return CGSizeMake(210.0f, paddingTop+heightTitle+heightImage+heightMsgView+magicDelta);
}
- (NSUInteger)mediaHash
{
return self.hash;
}
- (NSString *)mediaDataType
{
return (NSString *)kUTTypeMessage;
}
- (id)mediaData
{
return UIImageJPEGRepresentation(self.image, 1);
}
#pragma mark - NSObject
- (NSUInteger)hash
{
return super.hash ^ self.image.hash;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: image=%@, appliesMediaViewMaskAsOutgoing=%@>",
[self class], self.image, @(self.appliesMediaViewMaskAsOutgoing)];
}
#pragma mark - NSCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
_image = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(image))];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:self.image forKey:NSStringFromSelector(@selector(image))];
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
EventMediaItem *copy = [[EventMediaItem allocWithZone:zone] initWithImage:self.image title:self.title message:self.message];
copy.appliesMediaViewMaskAsOutgoing = self.appliesMediaViewMaskAsOutgoing;
return copy;
}
@end
使用方法:首先注册cell nib(Swift项目)
self.collectionView.registerNib(UINib(nibName: "VobMessagesMediaCellIncoming", bundle: nil), forCellWithReuseIdentifier: "VobMessagesMediaCellIncoming")
VobMessagesMediaCellIncoming 只是您现在正在使用的任何传入消息单元的副本,根据我的经验,您必须复制一个具有不同标识符的新单元,而不是对普通消息和媒体消息使用相同的 Nib ,否则 JSQMessageCollectionView 可能会混淆并在重复使用电池时显示异常信息。
然后,在 cellForItemAtIndexPath 中,您将 fork 逻辑以不设置单元格的 textView,因为相应的消息是 Media-type(EventMediaItem) 并且单元格的 textView 的任何设置都会导致断言失败:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = viewModel.messageBubbleModelView(indexPath.item)
if !message.isMediaMessage {
let color = senderId == message.senderId ? UIColor.whiteColor() : AppColor.BodyTextColor
cell.textView.textColor = color
cell.textView.linkTextAttributes = [
NSForegroundColorAttributeName: color,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
}
//set position nickname
cell.messageBubbleTopLabel.textAlignment = .Left
let edgeInset = cell.messageBubbleTopLabel.textInsets
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(edgeInset.top, edgeInset.left - 20, edgeInset.bottom, edgeInset.right + 20)
...
}
顺便说一句,isMediaMessage 属性来自 JSQMessage,如果你初始化一个 JSQMessage,它将被设置为 ture;最后,在您的自定义 JSQMessage 中:(MessagePost是我自己项目的消息模型)
class MessageBubbleViewModel: JSQMessage {
let messagePost: MessagePost
init(messagePost: MessagePost) {
self.messagePost = messagePost
if messagePost.isEvent {
let eventMedia:EventMediaItem = EventMediaItem(image: UIImage(named: "some cool image")!, title: messagePost.title, message: messagePost.msg)
super.init(senderId: String(messagePost.senderId),
senderDisplayName: messagePost.senderNickName,
date: messagePost.updatedAt,
media: eventMedia)
}else{
super.init(
senderId: String(messagePost.senderId),
senderDisplayName: messagePost.senderNickName,
date: messagePost.updatedAt,
text: messagePost.text)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
似乎还有很多其他的方法可以实现这个,你可以引用这个链接: https://github.com/jessesquires/JSQMessagesViewController/issues?utf8=%E2%9C%93&q=+%5Bcustom+cells%5D+in%3Atitle但是,我无法从这些线程中获得任何有用的信息。 --#
关于ios - JSQMessages 的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821699/
我试图要求 test/unit 的新版本(即不是与 ruby 捆绑的版本) .根据 instructions我用 gem i test-unit 安装它.但是现在当我需要 test/unit我似乎
简短版本是: 我有一个 systemd 单元,我想在调用时检查脚本的返回代码: systemctl status service.service 长版本:我有一个 lsb init 脚本正是这样做的,
我正在使用反射动态创建一个类的实例,这工作正常,除非尝试通过单元测试执行此操作 - 我使用的是 MS 测试框架。 我收到熟悉的错误:“无法加载文件或程序集‘Assy’或其依赖项之一。系统找不到指定的文
我想知道测试网站“界面功能”的最佳实践是什么。 我对哪些方法可用于测试界面和动态生成的内容感兴趣。特别是,我很难弄清楚是否可以为需要用户交互的操作创建自动化测试,或者这是否只是浪费时间,我应该让一些
我有一个简单的 Python 服务,其中有一个无限执行某些操作的循环。在各种信号上,sys.exit(0) 被调用,这导致 SystemExit 被引发,然后如果可以的话应该进行一些清理。 在测试中,
我正在使用 OpenCV 2.4.2 这是 OpenCV 文档中的引文 C++: void HoughLinesP(InputArray image, OutputArray lines, doubl
忙于 C# 中的自动化测试用例,我们需要在每次测试后恢复数据库快照。问题是,当运行多个测试时它会失败,因为“其他用户正在使用数据库时无法更改数据库状态。” 我们使用 SqlConnection.Cle
我阅读了 C# 规范并用谷歌搜索了它,但一无所获。 我 99% 肯定 C# 中没有像单元命名空间指令这样的功能,但问题是:为什么?是否有惯用或技术原因? 这很方便,尤其是当我们的大部分文件都由单个命名
我目前正在尝试向我的应用程序(一个非常老的项目......评论说 iOS 2.0)添加单元测试(精确的应用程序测试)并且总是偶然发现 undefined symbols for architectur
我正在使用Delphi 7,并且有一个新单元要在我的项目中使用。我已经编译了新的。当我尝试通过将其添加到uses子句在项目中使用此单元时,出现错误,提示未找到.dcu文件。我还尝试将文件的完整路径放在
场景:我需要编写一个复杂的nHibernate查询,该查询将返回预计的DTO,但是我想使用TDD方法。该方法如下所示: public PrintDTO GetUsersForPrinting(int
您可以通过运行以下命令在事件 html 设置中显示 Jupyter 笔记本: $ jupyter nbconvert untitled.ipynb --to slides --post serve 有
如何在一个网站上拥有多个 AdSense 单元? Google 提供的唯一代码是按单位计算的。 (adsbygoogle = window.adsbygoogle || []).push({})
我刚刚开始为大量代码编写测试。有很多类依赖于文件系统,即读取 CSV 文件、读/写配置文件等。 当前测试文件存储在项目(这是一个 Maven2 项目)的 test 目录中,但由于多种原因该目录并不总是
我对 TDD 还很陌生,在单元测试方面也不是很老练,所以才有这个问题。我有这个用 PHP 编写的遗留函数 function foo(){ x = bar(); y = baz();
我创建了一个程序,在 Swing 窗口的一侧显示结果过滤选项,但默认情况下它们水平相邻显示,这浪费了我在 BorderLayout 的西侧分配的空间。我可以在构造函数或添加语句中传递任何内容来将它们堆
标题不好的借口:如果有人能更好地描述它,请做。 我有一个 WeakList类,它“基本上”是一个 List> (虽然不是字面意义上的派生自列表,但它应该对用户完全透明)。 现在的基本思想是“如果引用的
我正在尝试在 UITableView 上添加两个原型(prototype)单元。但是,我不知道如何验证是否能够为每个原型(prototype)“返回”正确的单元格。你们能帮我一下吗? func ta
我正在使用 CloudKit 作为数据库创建一个简单的待办事项列表应用程序。目前我可以添加和删除对象,但对编辑对象感到困惑。 编辑项目 Controller protocol EditItemCont
我正在针对以下任务训练 RNN:给定一个包含 30 个单词的序列,然后将该序列分类为二进制类。 在我的网络中拥有超过 30 个单元(LSTM、GRU 或普通 RNN)有好处吗?我在网上看到过很多例子,
我是一名优秀的程序员,十分优秀!